Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Register Now and get your question answered!
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Cursor with ?dynamic? colum
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 04/12/2013 :  17:05:33  Show Profile  Reply with Quote
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
    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

Yak Posting Veteran

54 Posts

Posted - 04/12/2013 :  21:14:54  Show Profile  Reply with Quote
Some thing like

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

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

exec (@script)

Edited by - UnemployedInOz on 04/12/2013 21:15:19
Go to Top of Page

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 04/13/2013 :  01:46:44  Show Profile  Reply with Quote
use sp_executesql which can return values

SQL Server MVP
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000