tsql

Why NOT IN (NULL) Always returns nothing

大兔子大兔子 提交于 2020-01-22 00:23:07
问题 I have following table: And following simple query: SELECT * FROM dbo.Calendars WHERE calname NOT IN(NULL) My question is why always NOT IN(NULL) return nothing? PS :No matter what is your table,if you add NOT IN(NULL) to any column's condition,result is nothing. Thank you in advance :) 回答1: Because value equals NULL is not defined, and will never evaluate to True. Check This post on bad practices. Don't use IN and NULL , use EXISTS to test for at least one row where something exists. 回答2:

Profiling statements inside a User-Defined Function

淺唱寂寞╮ 提交于 2020-01-21 15:56:06
问题 I'm trying to use SQL Server Profiler (2005) to track down some application performance problems. One of the calls being made is to a table-valued user-defined function. This function wraps a select that joins several tables together. In SQL Server Profiler, the call to the UDF is logged. However, the select that underlies the UDF isn't being logged at all. Because of this, I'm not getting useful data on which tables & indexes are being hit. I'd like to feed this info into the Database Tuning

How automatically add 1 year date to an existing date in SQL Server

被刻印的时光 ゝ 提交于 2020-01-21 11:24:23
问题 I have a task to automatically bill all registered patients in PatientsInfo table an Annual Bill of N2,500 base on the DateCreated column. Certainly I will use a stored procedure to insert these records into the PatientDebit table and create a SQL Job to perform this procedure. How will I select * patients in PatientsInfo table where DateCreated is now 1 yr old for me to insert into another table PatientDebit . I have my algorithm like this: select date of registration for patients from

How can I query 'between' numeric data on a not numeric field?

怎甘沉沦 提交于 2020-01-21 11:06:30
问题 I've got a query that I've just found in the database that is failing causing a report to fall over. The basic gist of the query: Select * From table Where IsNull(myField, '') <> '' And IsNumeric(myField) = 1 And Convert(int, myField) Between @StartRange And @EndRange Now, myField doesn't contain numeric data in all the rows [it is of nvarchar type]... but this query was obviously designed such that it only cares about rows where the data in this field is numeric. The problem with this is

SQL Group by Year

一个人想着一个人 提交于 2020-01-21 06:18:26
问题 This is my query. select CONVERT(varchar, cast(date as datetime), 3) from shoptransfer group by year (date) I want to group by the year part of the date (varchar) column, however I get the following error: Column 'shoptransfer.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. How do I group by the year part of the date column? 回答1: How about: select datepart(yyyy, [date]) as [year] from shoptransfer group by datepart(yyyy,

while loop inside a trigger to loop through all the columns of table in sql

こ雲淡風輕ζ 提交于 2020-01-21 01:52:28
问题 I have a trigger like below on user table to insert into the audit table with which column was updated and previous value: ALTER TRIGGER [dbo].[trgAfterUpdate] ON [dbo].[tbl_User] AFTER UPDATE AS declare @fieldname varchar(128) ; declare @OldValue varchar(255); declare @CreateUser varchar(100) ; declare @User_Key int; select @CreateUser =i.user_name from deleted i; SELECT @User_Key = i.user_key from inserted i; if update(user_name) begin select @OldValue=j.user_name from deleted j; set

How to find out user name and machine name to access to SQL server

旧巷老猫 提交于 2020-01-21 01:42:19
问题 My work company has a MSSQL server 2005. I have two questions about finding out current log user and any way to send out a warning message: First question is if there is any T-SQL or SP available to find out current login user name and machine name. If the user is using SQL server sa name to remotely access to SQL server, is there any way to find out that user's windows name (the name to log to the windows)? My next question is that if I can get the user name or id, is there any way to send

Sql server - recursive delete

淺唱寂寞╮ 提交于 2020-01-20 17:30:11
问题 I'm trying to delete user's data and all it's related data that is located in different tables. All the tables have Foreign Keys but without cascade delete. I investigated some options: Enable cascade delete on all FK, delete and remove the cascade delete. Delete from bottom UP, loop up for all the leaves delete and repeat this operation till Root. Are there any more smart option or other Techniques? I'm using Microsoft SQL Server 2012 (SP1) 回答1: Oracle solution: How to generate DELETE

How do I order by parent then child?

社会主义新天地 提交于 2020-01-20 03:59:05
问题 I am trying to write my SQL Server 2008 query in such a way that I can just loop through my output and output headers as needed. I've done this stuff the wrong way many times and had ColdFusion do the hard work within the page, but need this done in SQL Server. FeatureID ParentID Feature -------------------------- 1 0 Apple 2 0 Boy 3 2 Charles 4 1 Daddy 5 2 Envelope 6 1 Frankfurter I want my query resultset to look like this: FeatureID ParentID Feature -------------------------- 1 0 Apple 4 1

Sum of child levels total in a Hierarchy

若如初见. 提交于 2020-01-20 02:48:05
问题 I need to have each level be the sum of all children (in the hierarchy) in addition to any values set against that value itself for the Budget and Revised Budget columns. I've included a simplified version of my table structure and some sample data to illustrate what is currently being produced and what I'd like to produce. Sample table: CREATE TABLE Item (ID INT, ParentItemID INT NULL, ItemNo nvarchar(10), ItemName nvarchar(max), Budget decimal(18, 4), RevisedBudget decimal(18, 4)); Sample