dbTalk Databases Forums  

how to write one stored procedure for a (same column )present in 20 tables

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss how to write one stored procedure for a (same column )present in 20 tables in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
satish
 
Posts: n/a

Default how to write one stored procedure for a (same column )present in 20 tables - 03-08-2006 , 05:13 AM






hi,
i am a learner of ms -sql server 2000, i had a doubt in stored
procedures

suppose i have a data base having 20 tables, all the tables have a
column named--DATE

can we write a store procdure to find out the data ---i mean i want
the data entered
between two days ---- if i call the stored procedure in any one of the
table i need to get the answer

pls help me how to write the stored procedure

satishkumar.g


Reply With Quote
  #2  
Old   
MC
 
Posts: n/a

Default Re: how to write one stored procedure for a (same column )present in 20 tables - 03-08-2006 , 05:34 AM






Do you mean calling the procedure for any one table and then getting the
data from the table with a date filter? If yes, then you would need to use
dynamic string and send the table name and two dates to the stored procedure
as parameters. Something like

create procedure dbo.GetData
(
@tablename varchar(100),
@dateFrom datetime,
@dateTo datetime
)

as

execute
('
select * from '+@tablename +' where date >= ' + @dateFrom +' and date <= '+
@dateTo
)

go

offcourse, you should check the part with dates and see what would you
actually want to get back. Do you want to filter by time, should it include
or exclude datefrom data in the results and so on. You should also consider
using sp_executesql instead of execute.



MC



"satish" <satishkumar.gourabathina (AT) gmail (DOT) com> wrote

Quote:
hi,
i am a learner of ms -sql server 2000, i had a doubt in stored
procedures

suppose i have a data base having 20 tables, all the tables have a
column named--DATE

can we write a store procdure to find out the data ---i mean i want
the data entered
between two days ---- if i call the stored procedure in any one of the
table i need to get the answer

pls help me how to write the stored procedure

satishkumar.g




Reply With Quote
  #3  
Old   
Doug
 
Posts: n/a

Default Re: how to write one stored procedure for a (same column )present in 20 tables - 03-08-2006 , 04:35 PM



MC's answer is awesome IMO.

Only thing I might add is that this is called "Dynamic SQL."

For sure has its uses. Erland has some excellent writeups regarding how
easy it is to overuse this technique.

Obvious questions come up such as "Why do you have 20 tables all with
the same data?"


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.