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.
| Author |
Topic |
|
kami909
Starting Member
3 Posts |
Posted - 2005-12-21 : 16:40:39
|
hello every one i want to know how should i use dynamic table name to get data from the table like i want user to select name of the table & my query should return the the data from the selected table i m trying this declare @MyTable nvarchar(50)set @MyTable='Customers'select * from @MyTable but its not working :-( later i want user to select colums as well i want to perform all this query in stored procedure any idea ? i m using sql server 2000 thanx any way .. |
|
|
jhermiz
3564 Posts |
|
|
shallu1_gupta
Constraint Violating Yak Guru
394 Posts |
Posted - 2005-12-21 : 22:50:14
|
| try this..create proc dynamicsql (@tablename varchar(100))asdeclare @sqlstring varchar(200)set @sqlstring= 'Select * from ' + @tablenameexec (@sqlstring)subsequently u can replace '*' by a variable having column names |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2005-12-21 : 23:17:31
|
| you may also use sp_executesql.Refere to Books Online for details.-----------------[KH]Learn something new everyday |
 |
|
|
|
|
|