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.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Cursor with ?dynamic? colum

Author  Topic 

SrPuma
Starting Member

1 Post

Posted - 2013-04-12 : 17:05:33
The subject title might be simple but im not sure the name for this since what im trying to reach is a way to be able to see if a column has a certain value:

FETCH NEXT FROM Tabs INTO @table_name, @column_name
WHILE....
BEGIN
Set @existing=0
SELECT @existing = count([@column_name])
FROM @table_name
WHERE [@column_name]=@new


so far my issue is the @column_name, which says its a invalid colum name (where i check if the @new i give exists in the records) for this purpose and the @table_name gives me the same issue, since im trying to have the cursor check for a certain table that has a FK colum name i give s PK, having this query working to open the cursor

UnemployedInOz
Yak Posting Veteran

54 Posts

Posted - 2013-04-12 : 21:14:54
Some thing like

Create Table #Info (ColName varchar(max),cnt int)
declare @script varchar(max)

FETCH NEXT FROM Tabs INTO @table_name, @column_name
WHILE....
BEGIN
Set @existing=0
select @script = 'Insert Into #Info SELECT ''' + @column_name + ''',count([' + @column_name + ']) FROM ' + @table_name + ' WHERE [' + @column_name + ']= ' + @new

exec (@script)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-13 : 01:46:44
use sp_executesql which can return values

http://msdn.microsoft.com/en-us/library/ms188001.aspx



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -