Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Dynamic SQL insert NULL values

Author  Topic 

Luuk123
Yak Posting Veteran

52 Posts

Posted - 2014-04-09 : 07:46:18
Hi all,

I have a dynamic SQL query like this:

declare @var1 varchar(100) = 'Henry',
@var2 varchar(100)
insert into tab (col1, col2)
select @var1, @var2


As you see @var2 is NULL. How can I insert a NULL value with dynamic SQL?

Thank you!

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-04-09 : 08:44:47
Not quite sure what the problem is. Your code works for me:


declare @sql nvarchar(max) = '
declare @t table (col1 varchar(100), col2 varchar(100));
declare @var1 varchar(100) = ''Henry'',
@var2 varchar(100);
insert into @t (col1, col2)
select @var1, @var2;
select * from @t
'
exec (@sql)


Results:



(1 row(s) affected)
col1 col2
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
Henry NULL

(1 row(s) affected)
Go to Top of Page
   

- Advertisement -