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)
 what is Dynamic sql syntax of this query

Author  Topic 

ranjeetsingh_6
Posting Yak Master

125 Posts

Posted - 2008-02-27 : 01:11:06
Hi,

I am using a simple if staement in a dynamic sql query what is its synax.

if @docType ='Doctor'
begin
select * from @tablename
end

Ranjeet Kumar Singh

mahesh_bote
Constraint Violating Yak Guru

298 Posts

Posted - 2008-02-27 : 01:14:17
you r going right. in Dynamic SQL u need to just concatenate the Select statement. e.g.

if @docType ='Doctor'
begin
@strSQL = 'select * from ' + @tablename
end


thanks,

Mahesh
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2008-02-27 : 01:16:48
the syntax u posted is accurate if tablename was the actual declared name of a table.

I believe u are looking for

If @doctype = 'whatever'
begin
exec('select * from ' + @tablename)
end
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-27 : 01:38:50
Why do you want to pass table name as parameter?
Make sure you read this fully
www.sommarskog.se/dynamic_sql.html

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -