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
 General SQL Server Forums
 New to SQL Server Programming
 select from a table if it name passed as parameter

Author  Topic 

medivhk3
Starting Member

4 Posts

Posted - 2010-02-17 : 17:38:30
Hello, my problem is as follows:

I have to create a stored procedure in which I receive a parameter that contains the name of a table, this parameter must be used to then select all rows from that table, first i thought something like:


create procedure sp_GetAllRows @TableName varchar(100)
as
select * from @TableName
go


would do the job, but oh well, it didn't (it was too beautiful to be true).

Can you guys help me get this done? I'm kinda clueless right now.

Thanks in advance

medivhk3
Starting Member

4 Posts

Posted - 2010-02-17 : 18:03:11
Well, now I feel silly, should've read the FAQ sticky before posting... found the answer to my problem here:

http://www.sqlteam.com/article/dynamic-sql-or-how-do-i-select-top-var-records

just to leave some evidence, my testing of those principles ended up like this:

declare @tablename varchar(50)
declare @sqlstatement varchar(150)

set @tablename = 'Test'
set @sqlstatement = 'select * from ' + @tablename

Execute (@sqlstatement)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-02-17 : 18:13:45
This is not a good idea at all. There is no point in using stored procedures if you are going to write code like that. You might as well use inline sql in your application instead.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

medivhk3
Starting Member

4 Posts

Posted - 2010-02-17 : 19:25:42
This restriction comes from the architecture itself, in which i can't access the db, or any data, directly, it must be accessed by the classes provided, and to use those classes i must first register a transaction which points to a certain database and a stored procedure. So to get any data, i must first create a SP and then register it, then use it via the classes provided.

It's kinda elaborated and it has it's pros and cons, but anyway, that's how i have to work and really there is no way out of it, i appreciate the suggestion.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-02-17 : 19:26:54
You should be warned that there are performance and security implications with a stored procedure like that.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-18 : 01:25:01
Make sure you read this fully
www.sommarskog.sq/dynamic_sql.html

Madhivanan

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

medivhk3
Starting Member

4 Posts

Posted - 2010-02-18 : 12:32:17
@tkizer: thought so, sadly, time is of the essence and i can't spend much time figuring out what would be the best way to do that with the best performance, also, in regards of security, what would the risks be? (i could only think of SQL Injection but proper SQL Sanitization methods are in place, or so i've been told -need to check that out)

@madhivanan: thanks for the link, though i can't check it out at work, Websense is blocking it, so i'll have to view it at home.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-02-19 : 01:02:29
quote:
Originally posted by medivhk3

@tkizer: thought so, sadly, time is of the essence and i can't spend much time figuring out what would be the best way to do that with the best performance, also, in regards of security, what would the risks be? (i could only think of SQL Injection but proper SQL Sanitization methods are in place, or so i've been told -need to check that out)

@madhivanan: thanks for the link, though i can't check it out at work, Websense is blocking it, so i'll have to view it at home.


Ask your Network Admin to unblock it

Madhivanan

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

- Advertisement -