Author |
Topic |
saketg
Starting Member
8 Posts |
Posted - 2012-01-09 : 15:01:27
|
Hi,I have a nested cursor. The outer cursor is iterating through all Columns in a table. Then I use the column name fetched from cursor and stored in a variable to build query for the inner cursor.SQL Server works for:SELECT COUNT(@myColumnName) FROM myTable;But fails to work for:SELECT COUNT(DISTINCT @myColumnName) FROM myTable;It always returns answer as 1. Cursor is getting all Column names because I am printing those. Moreover, if I copy the query from output window and run, it gives correct output.This issue exists for int and bit data types.Similarly, the query below doesn't work:SELECT COUNT(@myColumnName) FROM myTable WHERE @myColumnName = 1;Am I missing something? Please help.Thanks. |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
X002548
Not Just a Number
15586 Posts |
|
saketg
Starting Member
8 Posts |
Posted - 2012-01-09 : 16:49:15
|
Hi Tara,Thank you very much. Now the query executes correctly. Though, is there a way to save the output of this to a variable?Thanks. |
 |
|
X002548
Not Just a Number
15586 Posts |
|
saketg
Starting Member
8 Posts |
Posted - 2012-01-09 : 17:11:04
|
quote: Originally posted by X002548 The outer cursor?Post the cursors that you have alreadyWhat are you going to do with the cursor results? Updates, inserts, deletes? Reports to a temp table?What?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/
Hi Brett,My code is:USE CorrectDBDECLARE @myID INTEGER;DECLARE @myColumnName VARCHAR(250);DECLARE @myDataType VARCHAR(250);DECLARE @myResult INTEGER;DECLARE @myPrint VARCHAR(250);DECLARE cMasterTable CURSOR FOR SELECT IDFROM tblMasterORDER BY ID; OPEN cMasterTable FETCH NEXT FROM cMasterTable INTO @myID;WHILE @@FETCH_STATUS = 0 BEGIN DECLARE cAllColumns CURSOR FOR SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'tblDetails' ORDER BY ORDINAL_POSITION; OPEN cAllColumns FETCH NEXT FROM cAllColumns INTO @myColumnName, @myDataType; WHILE @@FETCH_STATUS = 0 BEGIN IF @myDataType = 'int' OR @myDataType = 'nvarchar' BEGIN DECLARE cDetailsTable1 CURSOR FOR SELECT COUNT(DISTINCT @myColumnName) FROM tblDetails WHERE ID = @myID AND @myColumnName IS NOT NULL; OPEN cDetailsTable1 FETCH NEXT FROM cDetailsTable1 INTO @myResult; IF @myResult > 0 BEGIN SELECT @myPrint = STR(@myID) + ' - ' + @myColumnName + ' - ' + STR(@myResult); PRINT @myPrint; END; CLOSE cDetailsTable1; DEALLOCATE cDetailsTable1; END; ELSE IF @myDataType = 'bit' BEGIN DECLARE cDetailsTable2 CURSOR FOR SELECT COUNT(DISTINCT @myColumnName) FROM tblDetails WHERE ID = @myID; OPEN cDetailsTable2 FETCH NEXT FROM cDetailsTable2 INTO @myResult; IF @myResult > 1 BEGIN SELECT @myPrint = STR(@myID) + ' - ' + @myColumnName + ' - ' + STR(@myResult); PRINT @myPrint; END; CLOSE cDetailsTable2; DEALLOCATE cDetailsTable2; END; FETCH NEXT FROM cAllColumns INTO @myColumnName, @myDataType; END; CLOSE cAllColumns; DEALLOCATE cAllColumns; FETCH NEXT FROM cMasterTable INTO @myID; END;CLOSE cMasterTable;DEALLOCATE cMasterTable;Basically I have a huge table having 150+ columns. One of the fields is linked to a master table. I have to find out which columns have values for at least one row for every entry in Master table. I am trying to run the outer cursor for all entries in Master table. Second cursor on all columns present and then the inner conditions that are failing are just checking number of distinct values as per data type.Based on Tara's suggestion, when I update cDetailsTable1 to dynamic sql then I get results but I have to check the condition also.Hope this help.Thanks. |
 |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
saketg
Starting Member
8 Posts |
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|