March 12, 2010 at 6:23 am
i want to create a stored procedure
which takes tablename as input and returns table data as out put
create procedure sp1(@tablename varchar(20)) returns table
as
return(select * from @tablename)
i've also tried
return('select * from' +@tablename)
its not working ....
March 12, 2010 at 6:39 am
--for users who are too lazy to type "SELECT * FROM TABLENAME"
CREATE procedure sp_show
--USAGE: sp_show TABLENAME
@TblName varchar(128)
As
Begin
exec('Select * from ' + @TblName)
End
Lowell
March 12, 2010 at 6:46 am
Not sure this is what you are after but the following syntax will create you a stored procedure taking a table name as input and will output the contents:
CREATE PROC [dbo].[ProcTest]
@TableSYSNAME
AS
BEGIN
DECLARE @SelectSyntaxNVARCHAR(200)
SET @SelectSyntax = 'SELECT * FROM ' + @Table
EXEC sp_executeSQL @SelectSyntax
END
Execute as follows:
EXEC [dbo].[ProcTest] @Table = '{table}'
Hope this helps
March 12, 2010 at 4:49 pm
Spencer Evans (3/12/2010)
Execute as follows:
EXEC [dbo].[ProcTest] @Table = '{table}'
Hope this helps
No, better like this:
EXEC [dbo].[ProcTest] @Table = '{table}; exec sp_resetpassword ... '
Just to make sure you take control over those optimists' server.
For those who is not ready to hand their servers to smart strangers it should look like this:
CREATE PROC [dbo].[ProcTest]
@Table SYSNAME
AS
BEGIN
DECLARE @SelectSyntaxNVARCHAR(200)
SET @Table = QUOTENAME(Object_Name(Object_ID(@Table, 'U')) )
IF @Table IS NULL
Report SQL injection here
ELSE
BEGIN
SET @SelectSyntax = 'SELECT * FROM ' + @Table
EXEC sp_executeSQL @SelectSyntax
END
END
_____________
Code for TallyGenerator
March 12, 2010 at 9:21 pm
r.vijaykanth (3/12/2010)
i want to create a stored procedurewhich takes tablename as input and returns table data as out put
Why? What is the business reason for this?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 17, 2010 at 3:59 am
thank u very much....
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply