Execute Dynamic SQL commands in SQL Server

In some applications having hard-coded SQL statements is not appealing, because of the dynamic nature of the queries being issued against the database server. Because of this sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can be done quite simply from the application perspective where the statement is built on the fly whether you are using ASP.NET , ColdFusion or any other programming language. But how do you do this from within a SQL Server stored procedure? SQL Server offers a few ways of running a dynamically built SQL statement. These ways are: Writing a query with parameters Using EXEC Using sp_executesql Writing a query with parameters This first approach is pretty straightforward if you only need to pass parameters into the WHERE clause of your SQL statement. Let’s say we need to find all records from the Customers table where City = ‘London’. This can be done easily as the following example shows.

How To Delete a null record

The code below will show you how exactly you delete a row with a NULL value. You can not use =NULL but you have to use IS NULL

CREATE TABLE #TestDeleteNull (id INT identity, SomeDate DATETIME)
INSERT #TestDeleteNull VALUES(GETDATE())
INSERT #TestDeleteNull VALUES(GETDATE())
INSERT #TestDeleteNull VALUES(NULL)


--Check what's in the table
SELECT * FROM #TestDeleteNull

--This won't work
DELETE #TestDeleteNull
WHERE SomeDate = NULL
--(0 row(s) affected)

--This is the way
DELETE #TestDeleteNull
WHERE SomeDate IS NULL
--(1 row(s) affected)

--Check again
SELECT * FROM #TestDeleteNull

Comments

Anonymous said…
great code. thanks.
Unknown said…
thank a lot..it helped me
Unknown said…
Thanks. It helped.
thankfully gave a tips
rduke131 said…
thanks a lotttttttttttttt.it helped me
Unknown said…
Very Nice, Thanks A Lot...
Richard Scott said…
Great help - thanks for being succinct and clear
Richard Scott said…
Thanks - perfect for what I needed - I apprecaite you keepin it simple and succinct.
Tejaswini said…
Thanks alot yar....
OpenSAGA said…
Thank u so so much!!!!
OpenSAGA said…
Thank u so so much!!!
This comment has been removed by the author.
Perma Frost said…
thanks alot! saved alot of time!
JideshDavid said…
Thank u so much...
JideshDavid said…
This comment has been removed by the author.
J9139213 said…
Good Stuff - Thanks
Unknown said…
thanks sir.....
Mario B. said…
thanks for the advice, i was stuck
Mario B. said…
thanks i was stuck
Unknown said…
maarbanee bai jaan
Unknown said…
Awesome piece of knowledge, thanks bro..It solved my issue in less than 5 min..Thanks for the great help.
Unknown said…
Hi, This code is great, but it is deleating the whole line. What IF I Just want to Delete the NULL, and keep the rest of the row: For example in this code I would like to delete Null, but keep 't', AND 's'

CREATE TABLE #TestDeleteNull (id INT identity, SomeDate DATETIME, Comments text)
INSERT #TestDeleteNull VALUES(GETDATE(), 't' )
INSERT #TestDeleteNull VALUES(GETDATE(), 'f')
INSERT #TestDeleteNull VALUES(NULL, 't')
INSERT #TestDeleteNull VALUES(NULL, 's')



DELETE #TestDeleteNull
WHERE SomeDate IS NULL

SELECT * FROM #TestDeleteNull

Popular posts from this blog

Check If Temporary Table Exists

Multiple NULL values in a Unique index in SQL

Row To Column