Please start any new threads on our new site at http://forums.sqlteam.com. 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!
Username:
Password:
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  

SrPuma
Starting Member

Portugal
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
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

Australia
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
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)

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

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 04/13/2013 :  01:46:44  Show Profile  Reply with Quote
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
  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.33 seconds. Powered By: Snitz Forums 2000