Re: Cursor Error...
From: Dan Guzman (guzmanda_at_nospam-online.sbcglobal.net)
Date: 04/01/05
- Next message: marcmc: "Re: Cursor Error..."
- Previous message: marcmc: "Cursor Error..."
- In reply to: marcmc: "Cursor Error..."
- Next in thread: marcmc: "Re: Cursor Error..."
- Reply: marcmc: "Re: Cursor Error..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Date: Fri, 1 Apr 2005 08:14:12 -0600
The SQL Server parser doesn't resolve variables as table/column names.
You'll need to use dynamic SQL to do this in Transact-SQL.
See http://www.sommarskog.se/dynamic_sql.html for an article on dynamic SQL
considerations.
-- Hope this helps. Dan Guzman SQL Server MVP "marcmc" <marcmc@discussions.microsoft.com> wrote in message news:38C354EE-43DC-42D8-B4B9-E503B72D34EA@microsoft.com... > ...I can't seem to work it out, maybe you can help. I get a > Server: Msg 137, Level 15, State 2, Line 22 > Must declare the variable '@tbl_name'. > when I run... > > declare @counter int, @tbl_name varchar(40) > select @counter = 1 > > declare loop_cursor cursor for SELECT distinct name FROM > sysobjects(nolock) > WHERE NAME in ( > 'marc', 'marc1') and xtype = 'U' > > open loop_cursor > fetch loop_cursor into @tbl_name > > WHILE @@fetch_status = 0 > BEGIN > UPDATE @tbl_name > SET marcA = substring(marcA,1,2) > WHERE marc = 1 > > PRINT @counter > PRINT '____________________________' > PRINT @tbl_name + 'Post Codes Updated' > PRINT ' ' > SELECT @counter = @counter + 1 > END > > FETCH loop_cursor INTO @tbl_name > CLOSE loop_cursor > DEALLOCATE loop_cursor > > > am i missing something blatently obvious, it's friday so probably so > arrrgh > > > The creates are as follows for anyone wishing to try it > drop table marc, marc1 > create table marc (marc int, marcA varchar(5)) > create table marc1 (marc int, marcA varchar(5)) > insert into marc values(1, 'ABC') > insert into marc values(2, 'ABC') > insert into marc1 values(1, 'ABC') > insert into marc1 values(2, 'ABC')
- Next message: marcmc: "Re: Cursor Error..."
- Previous message: marcmc: "Cursor Error..."
- In reply to: marcmc: "Cursor Error..."
- Next in thread: marcmc: "Re: Cursor Error..."
- Reply: marcmc: "Re: Cursor Error..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|