1

Is it possible in T-SQL to write a proper query reflecting this pseudo-code:

SELECT {primary_key}, {column_name}
FROM {table}
WHERE {any column_name value} is NULL

i.e. without referencing each column-name explicitly.

Sounds simple enough but I've searched pretty extensively and found nothing.

6
  • 2
    There's no shorthand for this - it's an unlikely requirement in most database designs. Commented Jul 22, 2011 at 14:50
  • I wrote an answer with the query to use for this. Since noone bothered to read or grade it, I deleted it and leaving it up to someone else to answer. Commented Jul 22, 2011 at 21:08
  • @t-clausen.dk - Stick with it, these things happen (in SO and in real-life). If you are confident in your answer, then leave it up. If it doesn't get votes now, then it may later. But if not, then it still shows you have the knowledge and character to pass on to others. Commented Jul 23, 2011 at 3:00
  • @CodeNaked somehow I think there is a problem with this system. If i find a 1 year old tough problem with 0 answers and give a proper answer. None will ever find the answer. Case of a problem like this one, is not a problem many people will ever encounter, so if someone hardcore enough bother solving it, noone cares. They are definitely not going to give points for a solution they are unlikely to ever use. Those hardcore people that could use the answer are normally unlikely to reward "the competition" with points it seems. The OP is a 1 rep guy and those almost never give credit Commented Jul 23, 2011 at 9:34
  • @t-clausen.dk - I'd encourage you to undelete the answer. Even if it doesn't satisfy the current questioner, it might answer a future user's search. Although it's disappointing to get no immediate feedback, it's sometimes quite nice to notice an uptick in your rep (6 months, a year later), and be brought back to revisit your answer because someone has now upvoted it. But if you're only answering for rep (as opposed to enjoying the exercise of solving the problems), I'm not sure what to suggest to fix that. Commented Jul 23, 2011 at 19:49

1 Answer 1

2

You have to use dynamic sql to solve that problem. I have demonstrated how it could be done. With this sql you can pick a table and check the row with id = 1 for columns being null and primary keys. I included a test table at the bottom of the script. Code will not display anything if there is not primary keys and no columns being null.

DECLARE @table_name VARCHAR(20)
DECLARE @chosencolumn VARCHAR(20)
DECLARE @sqlstring VARCHAR(MAX) 
DECLARE @sqlstring2 varchar(100)
DECLARE @text VARCHAR(8000)
DECLARE @t TABLE (col1 VARCHAR(30), dummy INT)

SET @table_name = 'test_table'  -- replace with your tablename if you want
SET @chosencolumn = 'ID=1'      -- replace with criteria for selected row

SELECT @sqlstring = COALESCE(@sqlstring, '') + 'UNION ALL SELECT '',''''NULL '''' '' + '''+t1.column_name+''', 1000 ordinal_position FROM ['+@table_name+'] WHERE [' +t1.column_name+ '] is null and ' +@chosencolumn+ ' ' 
FROM INFORMATION_SCHEMA.COLUMNS t1
LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE t2
ON t1.column_name = t2.column_name
AND t1.table_name = t2.table_name
AND t1.table_schema = t2.table_schema
WHERE t1.table_name = @table_name
AND t2.column_name is null

SET @sqlstring = stuff('UNION ALL SELECT '',''''PRIMARY KEY'''' ''+ column_name + '' '' col1, ordinal_position 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE table_name = ''' + @table_name+ '''' + @sqlstring, 1, 10, '') + 'order by 2'

INSERT @t
EXEC( @sqlstring)

SELECT @text = COALESCE(@text, '') + col1
FROM @t

SET @sqlstring2 ='select '+stuff(@text,1,1,'')

EXEC( @sqlstring2)

Result:

id           host_id      date         col1
PRIMARY KEY  PRIMARY KEY  PRIMARY KEY  NULL

Test table

CREATE TABLE [dbo].[test_table](
    [id] int not null,
    [host_id] [int] NOT NULL,
    [date] [datetime] NOT NULL,
    [col1] [varchar](20) NULL,
    [col2] [varchar](20) NULL,
 CONSTRAINT [PK_test_table] PRIMARY KEY CLUSTERED 
(
    [id] ASC,
    [host_id] ASC,
    [date] ASC
))

Test data

INSERT test_table VALUES (1, 1, getdate(), null, 'somevalue')  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.