473,440 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,440 software developers and data experts.

Help with Dynamic SQL

I want to use the following code below as Dynamic SQL because the first table name in the FROM clause, Staging, will change significantly. The code is part of a stored procedure that accepts the table name as an argument. How do I do that?

Expand|Select|Wrap|Line Numbers
  1.  
  2.       INSERT INTO StagingFinal(col1, col2,
  3.  
  4.                                col3,col4, col5, col6, col7, col8)
  5.  
  6.                SELECT stag.id,
  7.  
  8.                       REPLACE(stag.col1,stag.col1,'User' + stag.col1)
  9.  
  10.                     , us.id , ua.Users
  11.  
  12.                    , (CASE WHEN UPPER(Job) = 'Accounting' THEN 0 ELSE 1 END)
  13.  
  14.                     , stag.EndDate ,
  15.  
  16.                       CONVERT(INT, REPLACE(stag.earnings,'.000',''))
  17.  
  18.                     , stag.rep
  19.  
  20.                FROM Staging stag ,
  21.  
  22.                      dbo.Users us ,
  23.  
  24.                      dbo.UserAccs ua
  25.  
  26.                WHERE stag.cus = ''
  27.  
  28.                AND us.id = ua.id
  29.  
  30.                AND REPLACE(ua.AccountNumber,'-','') = stag.AccountNumber
  31.  
  32.  
  33.  
Jun 27 '07 #1
3 1382
vijaii
15
Use the below Stored Procedure as an example


--Stored Procedure to select data from the given table name parameter
CREATE PROCEDURE DYNAMICTABLENAME
(
@TABLENAME VARCHAR(100)
)
AS

DECLARE @strSQL NVARCHAR(4000)
DECLARE @strTableName VARCHAR(100)

set @strSQL = 'select * from '
set @strSQL = @strSQL + @TABLENAME
print @strSQL
EXEC sp_ExecuteSQL @strSQL


--To execute the Stored Procedure
EXEC DYNAMICTABLENAME @TABLENAME='TABLENAME'


I want to use the following code below as Dynamic SQL because the first table name in the FROM clause, Staging, will change significantly. The code is part of a stored procedure that accepts the table name as an argument. How do I do that?

Expand|Select|Wrap|Line Numbers
  1.  
  2.       INSERT INTO StagingFinal(col1, col2,
  3.  
  4.                                col3,col4, col5, col6, col7, col8)
  5.  
  6.                SELECT stag.id,
  7.  
  8.                       REPLACE(stag.col1,stag.col1,'User' + stag.col1)
  9.  
  10.                     , us.id , ua.Users
  11.  
  12.                    , (CASE WHEN UPPER(Job) = 'Accounting' THEN 0 ELSE 1 END)
  13.  
  14.                     , stag.EndDate ,
  15.  
  16.                       CONVERT(INT, REPLACE(stag.earnings,'.000',''))
  17.  
  18.                     , stag.rep
  19.  
  20.                FROM Staging stag ,
  21.  
  22.                      dbo.Users us ,
  23.  
  24.                      dbo.UserAccs ua
  25.  
  26.                WHERE stag.cus = ''
  27.  
  28.                AND us.id = ua.id
  29.  
  30.                AND REPLACE(ua.AccountNumber,'-','') = stag.AccountNumber
  31.  
  32.  
  33.  
Jun 27 '07 #2
Use the below Stored Procedure as an example


--Stored Procedure to select data from the given table name parameter
CREATE PROCEDURE DYNAMICTABLENAME
(
@TABLENAME VARCHAR(100)
)
AS

DECLARE @strSQL NVARCHAR(4000)
DECLARE @strTableName VARCHAR(100)

set @strSQL = 'select * from '
set @strSQL = @strSQL + @TABLENAME
print @strSQL
EXEC sp_ExecuteSQL @strSQL


--To execute the Stored Procedure
EXEC DYNAMICTABLENAME @TABLENAME='TABLENAME'

Thanks for your help Vijaii, I actually figure it out through trials and errors
Jun 27 '07 #3
srinit
43
I want to use the following code below as Dynamic SQL because the first table name in the FROM clause, Staging, will change significantly. The code is part of a stored procedure that accepts the table name as an argument. How do I do that?

Expand|Select|Wrap|Line Numbers
  1.  
  2.       INSERT INTO StagingFinal(col1, col2,
  3.  
  4.                                col3,col4, col5, col6, col7, col8)
  5.  
  6.                SELECT stag.id,
  7.  
  8.                       REPLACE(stag.col1,stag.col1,'User' + stag.col1)
  9.  
  10.                     , us.id , ua.Users
  11.  
  12.                    , (CASE WHEN UPPER(Job) = 'Accounting' THEN 0 ELSE 1 END)
  13.  
  14.                     , stag.EndDate ,
  15.  
  16.                       CONVERT(INT, REPLACE(stag.earnings,'.000',''))
  17.  
  18.                     , stag.rep
  19.  
  20.                FROM Staging stag ,
  21.  
  22.                      dbo.Users us ,
  23.  
  24.                      dbo.UserAccs ua
  25.  
  26.                WHERE stag.cus = ''
  27.  
  28.                AND us.id = ua.id
  29.  
  30.                AND REPLACE(ua.AccountNumber,'-','') = stag.AccountNumber
  31.  
  32.  

Hi , try like this
[code]
create procedure sp_name(@table varchar(20))
AS
exec('select * from '+@table)
Jun 28 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Marc | last post by:
Hello, I've fiddled with this for quite a while and thought I had the problem solved. I had a version that would successfully compile and run. But then I had to change the code to use a...
14
by: Stuart D. Gathman | last post by:
I have a function that recognizes PTR records for dynamic IPs. There is no hard and fast rule for this - every ISP does it differently, and may change their policy at any time, and use different...
10
by: Bob Rock | last post by:
Hello, is there a way to turn off dynamic help from displaying when clicking F1??? Every time I call help on a word the external MSDN opens up (just as I want) BUT also the dynamic pane inside...
0
by: Pat Patterson | last post by:
I'm having serious issues with a page I'm developing. I just need some simple help, and was hoping someone might be able to help me out in here. I have a form, that consists of 3 pages of...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
83
by: deppy_3 | last post by:
Hi.I am started learning Programm language C before some time.I am trying to make a programm about a very simple "sell shop".This programm hasn't got any compile problem but when i run it i face...
8
by: Dip | last post by:
Hello Experts, Here is the code to flatten a PC hierarchy into a level based table. It works fine. SELECT t1.TASK_ID AS TASK_LV1, t2.TASK_ID AS TASK_LV2, t3.TASK_ID AS TASK_LV3, t4.TASK_ID AS...
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, If I open the Dynamic help window in VS, why it doesn't show anything when I move a mouse to a system class say, dictionary. it says "No links are available for the current select"....
0
by: richard12345 | last post by:
Hi Guys I have problem with site I am building. The sidebar with menu and other thinks is overlapping footer. The footer move with the content and but it dos it dos not move with the sidebar. ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.