I have a stored procedure that accepts a string parameter, a comma separated list of required column names that the SP should check a particular table and make sure those columns have data in them… If any record has missing data in those columns the sp should return the recordID
and which columns having missing data.
I'm having trouble constructing the final query. I already split the comma separated list into a table variable. and am using a cursor to through each required column
DECLARE columnCursor CURSOR
FOR SELECT * FROM @columnsTable
OPEN columnCursor
DECLARE @currentColumnName nvarchar(256);
FETCH NEXT FROM columnCursor INTO @currentColumnName
WHILE(@@FETCH_STATUS = 0)
BEGIN
....
FETCH NEXT FROM columnCursor INTO @currentColumnName
END
Best Answer
Well you could do something like this which would avoid the cursor that you had in the query that you originally posted. Firstly, change the definition of your columns table so it looks like this:
Then, using whatever method you have; split the string and populate the table above. After which you could do something like this to get your desired results:
EDIT: Changed the body of the loop based on your comment below.
Be aware though that depending on the amount of rows you have in your table this could take a while...
I hope this helps you.