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 2000 Forums
 SQL Server Development (2000)
 incorporating joins in dynamic sql

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-05-22 : 08:06:35
vinay writes "can i get a solution how to incorporate joins in dynamic sql statements? for example,
i doing a join on two tables mentioned below

declare @tablename1 varchar(1000)
declare @tablename2 varchar(1000)
SET @TABLENAME2 ='PRODUCT'
set @tablename1='PRODUCTGROUP'
DECLARE @STR VARCHAR(2000)
SET @STR = 'SELECT * FROM ' + @TABLENAME2 +' AS ST' + 'JOIN'+ @tablename1 + ' AS PG ON ST.ROWID=PG.PRODUCTID
EXECUTE(@STR)

when i run this i get "Incorrect syntax near keyword AS""

Andraax
Aged Yak Warrior

790 Posts

Posted - 2003-05-22 : 08:21:52
Hi!

Insert a couple of blanks and you should do fine!

declare @tablename1 varchar(1000)
declare @tablename2 varchar(1000)
SET @TABLENAME2 ='PRODUCT'
set @tablename1='PRODUCTGROUP'
DECLARE @STR VARCHAR(2000)
SET @STR = 'SELECT * FROM ' + @TABLENAME2 +' AS ST ' + 'JOIN '+ @tablename1 + ' AS PG ON ST.ROWID=PG.PRODUCTID
EXECUTE(@STR)

I would recommend you to print the string when you debug, it's so much easier to find errors then!


Go to Top of Page
   

- Advertisement -