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 2005 Forums
 Transact-SQL (2005)
 Error in Execute command

Author  Topic 

shinoykurian
Starting Member

4 Posts

Posted - 2009-04-22 : 03:04:10
Hi guys, I am getting an error message for the below query, can some one help.

DECLARE tables_cursor CURSOR
FOR
SELECT RESCOD,ITMCOD FROM psmastbl WHERE rescod='GSH'
OPEN tables_cursor
DECLARE @restaurant varchar(3)
DECLARE @tablename decimal
FETCH NEXT FROM tables_cursor INTO @restaurant,@tablename
WHILE (@@FETCH_STATUS <> -1)
BEGIN
EXEC ('SELECT * FROM PSMASTBL WHERE RESCOD='+@restaurant)
print @tablename;
print @restaurant;
FETCH NEXT FROM tables_cursor INTO @restaurant,@tablename
END
CLOSE tables_cursor
DEALLOCATE tables_cursor

error
Msg 207, Level 16, State 1, Line 1
Invalid column name 'GSH'.
1
GSH
Msg 207, Level 16, State 1, Line 1
Invalid column name 'GSH'.
2

CREATE TABLE [dbo].[PSMASTBL](
[APPDAT] [decimal](8, 0) NOT NULL,
[RESCOD] [varchar](3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ITMCOD] [decimal](4, 0) NOT NULL,
[ITMNAM] [varchar](40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL DEFAULT (' '),
[SHTNAM] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL DEFAULT (' '),
PRIMARY KEY CLUSTERED
(
[APPDAT] ASC,
[RESCOD] ASC,
[ITMCOD] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO PSMASTBL( APPDAT, RESCOD, ITMCOD, ITMNAM, SHTNAM) VALUES ( 20090101, 'GSH', 1, 'TEST1', '222')
INSERT INTO PSMASTBL( APPDAT, RESCOD, ITMCOD, ITMNAM, SHTNAM) VALUES ( 20090101, 'GSH', 2, 'TEST2', '333')
INSERT INTO PSMASTBL( APPDAT, RESCOD, ITMCOD, ITMNAM, SHTNAM) VALUES ( 20090101, 'GSH', 3, 'TEST3', '444')

Thanks!
Shinoy

sanoj_av
Posting Yak Master

118 Posts

Posted - 2009-04-22 : 03:37:44
DECLARE tables_cursor CURSOR
FOR
SELECT RESCOD,ITMCOD FROM psmastbl WHERE rescod='GSH'
OPEN tables_cursor
DECLARE @restaurant varchar(3)
DECLARE @tablename decimal
FETCH NEXT FROM tables_cursor INTO @restaurant,@tablename
WHILE (@@FETCH_STATUS <> -1)
BEGIN

EXEC ('SELECT * FROM PSMASTBL WHERE RESCOD='''+@restaurant+'''')

FETCH NEXT FROM tables_cursor INTO @restaurant,@tablename
END
CLOSE tables_cursor
DEALLOCATE tables_cursor
Go to Top of Page

shinoykurian
Starting Member

4 Posts

Posted - 2009-04-22 : 03:47:37
Thanks very much.

Best Regards
Shinoy
Go to Top of Page
   

- Advertisement -