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 |
j0shua
Starting Member
40 Posts |
Posted - 2009-02-18 : 00:55:46
|
i have the following script that would select uniqueidentifiers from any table in the database. The problem is it will display all results regardless if there is no data. I am thinking to apply "if exists" however the command is stored in a variable. Is there a way to determine if data exists then it will not display or execute the code? DECLARE @searchCommand as VARCHAR(225)DECLARE searchAccount CURSOR SCROLL FOR (SELECT 'SELECT * FROM ' + QUOTENAME(TABLE_NAME) + ' WHERE ' + QUOTENAME(COLUMN_NAME) + '=''4898CC21-2EE6-4E0D-BD31-E8E53B52CF06''' AS COMMANDS FROM INFORMATION_SCHEMA.COLUMNS c WHERE DATA_TYPE='uniqueidentifier' AND EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME=c.TABLE_NAME and TABLE_TYPE='BASE TABLE'))OPEN searchAccount FETCH FIRST FROM searchAccount INTO @searchCommand WHILE @@FETCH_STATUS = 0 BEGIN EXECUTE(@searchCommand) PRINT @searchCommand FETCH NEXT FROM searchAccount INTO @searchCommand ENDCLOSE searchAccountDEALLOCATE searchAccount |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-02-18 : 03:44:48
|
Have you tried the system function @@CURSOR_ROWS ? E 12°55'05.63"N 56°04'39.26" |
|
|
j0shua
Starting Member
40 Posts |
Posted - 2009-02-18 : 04:17:26
|
not yet. what i am looking for is to determine if EXECUTE(@searchCommand) exists where (@searchCommand = 'SELECT * FROM table1'). if data exists when that command is called, then execute(@searchCommannd. The purpose of this is to avoid displaying tables with no data instead it will only display table with data on it. |
|
|
|
|
|