Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


creating procudre for dynamic table


creating procudre for dynamic table

Author
Message
raghuldrag
raghuldrag
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 473
Hi friends,
create tab T1(name varchar(22),age numeric(22))
create tab T2(name varchar(22),age numeric(22))
insert into t1 values('ram',22)
insert into t1 values('am',26)
insert into t1 values('sam',28)
insert into t1 values('bam',23)
insert into t1 values('kam',21)

insert into t2 values('yam',22)
insert into t2 values('dam',22)
insert into t2 values('gam',22)
insert into t2 values('pam',22)

now i need to create procedure if am giving the table name has input
show the output of name only!!!!

exec pro(t1)

expecting output:

NAME
ram
am
sam
bam
kam
ChrisM@home
ChrisM@home
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1161 Visits: 9735
raghuldrag (10/9/2012)
Hi friends,
create tab T1(name varchar(22),age numeric(22))
create tab T2(name varchar(22),age numeric(22))
insert into t1 values('ram',22)
insert into t1 values('am',26)
insert into t1 values('sam',28)
insert into t1 values('bam',23)
insert into t1 values('kam',21)

insert into t2 values('yam',22)
insert into t2 values('dam',22)
insert into t2 values('gam',22)
insert into t2 values('pam',22)

now i need to create procedure if am giving the table name has input
show the output of name only!!!!

exec pro(t1)

expecting output:

NAME
ram
am
sam
bam
kam



exec pro(t1) won't work, this will: exec pro('t1')

Assuming the table name is captured into parameter Tablename, your stored procedure could look something like this:

IF @Tablename = 'T1'
SELECT DISTINCT Name FROM t1
ELSE IF @Tablename = 'T2'
SELECT DISTINCT Name FROM t2

You could also use dynamic sql for this.


Low-hanging fruit picker and defender of the moggies





For better assistance in answering your questions, please read this.




Understanding and using APPLY, (I) and (II) Paul White

Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
PiMané
PiMané
Mr or Mrs. 500
Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)

Group: General Forum Members
Points: 592 Visits: 1334
ChrisM@home (10/10/2012)
You could also use dynamic sql for this.


CREATE PROCEDURE GetData(@TableName NVARCHAR(120), @Columns NVARCHAR(255))
AS
BEGIN
DECLARE @Query AS NVARCHAR(MAX)
SET @Query = 'SELECT ' + @Columns + ' FROM ' + @TableName
EXEC sp_executesql @Query
END
GO

EXEC GetData('t1', 'name')
GO


In this example you can specify the columns you want on the output...

Pedro



If you need to work better, try working less...
raghuldrag
raghuldrag
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 473
while executing dis query error occured on output:
exec
Arthur Kirchner
Arthur Kirchner
SSC-Enthusiastic
SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)

Group: General Forum Members
Points: 153 Visits: 398
It's

EXEC GetData 't1', 'name'
GO

... without parentheses.
raghuldrag
raghuldrag
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 473
if you dnt mind would please explain the "sp_executesql" what is the use??
and what should be do there??
PiMané
PiMané
Mr or Mrs. 500
Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)Mr or Mrs. 500 (592 reputation)

Group: General Forum Members
Points: 592 Visits: 1334
raghuldrag (10/10/2012)
if you dnt mind would please explain the "sp_executesql" what is the use??
and what should be do there??

Check this link on the site http://www.sqlservercentral.com/Forums/Topic1244180-1292-1.aspx.
There are few more just google "sp_executesql vs EXEC"...

Pedro



If you need to work better, try working less...
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search