Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sql and stored procedures

Status
Not open for further replies.

fcl22

Technical User
Apr 16, 2003
14
US
I have a stored procedure that accepts two arguments Begindate and enddate, I'm looking for a way to send the results of the procedure to a table from the procedure
@DbName
@tablename

is there a way to have the whole thing run at one time? if so can someone point me in the right direction.

thanks
fred
 
I'm not sure what you mean. You can insert the result of stored procedure execution into a table as in the following example.

Insert tablename
Exec sp_yourprocedure
@begindate='2003-01-01',
@enddate='2003-03-31'

If you want to pass the name of the table to the stored procedure and insert the result into that table, you'll need to use dynamic SQL.

Create Proc YourProcedure
@tablename varchar(40),
@begindate varchar(10),
@enddate varchar(10)
As

Set Nocount On

Declare @sql varchar(1000)
Select @sql =
'Insert ' + @tablename +
' Select col1, col2, col3, ..., colN' +
' From AnotherTable' +
' Where DateCol Between ' +
char(39) + @begindate + char(39) + ' And ' +
char(39) + @enddate + char(39)

Exec(@sql)

If these examples don't provide the answer you seek, please post more details.

If you want to get the best answer for your question read faq183-874 and faq183-3179.
Terry L. Broadbent - DBA
SQL Server Page:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top