Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Recently, my job need to design a generic application to query a lot of same format table within the sql server, in order to enhance the efficiency, I try to use procedure, however I don't know how to dynamic to input the table name parameter into the procedure. Have anyone can help me to solve it!

 

the following procedure code can't work, have any good idea to solve:

 


CREATE PROCEDURE TOTAL_TOPX_HANDLE AS SELECT COUNT(*) AS TOTAL FROM [color=Sienna]**Table_name** [/color] WHERE (PROFILE_STATUS <> 'A') AND (DAY(START_TIME) = DAY(GETDATE())) AND (MONTH(START_TIME) = MONTH(GETDATE())) AND (YEAR(START_TIME) = YEAR(GETDATE()))

Edited by philiplcp
Posted

(assuming SQL2K here):

You'll have to have your stored procedure query text (the actual SELECT statement) as a VarChar field in which you append the table name from your param passed into the proc.

 

Once you have the string build, you can EXEC that:

EXEC( @yourVarCharQueryStringGoesHere )

 

On a side note, this isn't really improving efficiency as far as sql performance as this will "compile" every time you run this proc, but it will increase maintenance efficiency for your developer/dba teams. To each, his own though. ;)

Posted
(assuming SQL2K here):

You'll have to have your stored procedure query text (the actual SELECT statement) as a VarChar field in which you append the table name from your param passed into the proc.

 

Once you have the string build, you can EXEC that:

EXEC( @yourVarCharQueryStringGoesHere )

 

On a side note, this isn't really improving efficiency as far as sql performance as this will "compile" every time you run this proc, but it will increase maintenance efficiency for your developer/dba teams. To each, his own though. ;)

 

 

thx mocella :D

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...