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.
Author |
Topic |
mp2_admin
Starting Member
10 Posts |
Posted - 2013-11-13 : 11:16:37
|
Hi,I am trying to get how many null fields i have in a row. But I am having trouble delcaring a variable (@tlbcnt) and using the variable in a subtraction routine below.. Can someone point out what I am doing incorreclt?Thank YouDECLARE @sql NVARCHAR(MAX) = ''DECLARE @tlbcnt NVARCHAR(MAX) = ''SET @tlbcnt = (Select COUNT(*) FROM sys.tables tJOIN sys.columns c ON t.object_id = c.object_id WHERE t.object_id = OBJECT_ID('r_recipes'));SELECT @sql = @sql + N' CASE WHEN '+QUOTENAME(c.name)+N' IS NULL THEN 1 ELSE 0 END +'FROM sys.tables tJOIN sys.columns c ON t.object_id = c.object_idWHERE c.is_nullable = 1AND t.object_id = OBJECT_ID('r_recipes')SET @sql = N'SELECT recipe_id, abs(('+@sql +N'+0)- +@tlbcnt) AS Cnt FROM [r_recipes] 'EXEC(@sql) |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2013-11-13 : 12:37:40
|
I'm not clear what you are trying to do, but I think you need to concatenate the variable into your string. For example:SET @sql = N'SELECT recipe_id, abs(('+@sql +N'+0)- ' + @tlbcnt + N') AS Cnt FROM [r_recipes] ' |
|
|
mp2_admin
Starting Member
10 Posts |
Posted - 2013-11-13 : 13:30:37
|
Ahh that is where i was messing up. Where I was concatenating my variables. Thank You. |
|
|
|
|
|