473,320 Members | 1,909 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,320 software developers and data experts.

sql parameters

I want to truncate a products_archive table and insert the products table into the products_archive table, then truncate the products table.
This has to be done for a few tables besides for the products table. How can I do this for all(using variables and parameters or something like that)
Thank you.
Feb 17 '11 #1

✓ answered by gpl

Your easiest option is to use 'dynamic sql' - this is where you build up your query in a string and then execute it

Expand|Select|Wrap|Line Numbers
  1. declare SQL_str varchar(2048)
  2. set SQL_Str='Select * from products'
  3. exec SQL_Str
you see yhat you could build a simple string and then execute it with a different name, for example
Expand|Select|Wrap|Line Numbers
  1. declare SQL_str varchar(2048)
  2. set SQL_Str=' truncate #table#_archive' +
  3. ' insert #table#_archive' +
  4. ' select * from #table#' +
  5. ' truncate #table#'
  6.  
  7. exec replace(SQL_Str, '#table#, 'products')
  8. exec replace(SQL_Str, '#table#, 'customers')
  9.  
this does reply on your archive tables being the same name as the main table with _archive appended

2 2142
gpl
152 100+
Your easiest option is to use 'dynamic sql' - this is where you build up your query in a string and then execute it

Expand|Select|Wrap|Line Numbers
  1. declare SQL_str varchar(2048)
  2. set SQL_Str='Select * from products'
  3. exec SQL_Str
you see yhat you could build a simple string and then execute it with a different name, for example
Expand|Select|Wrap|Line Numbers
  1. declare SQL_str varchar(2048)
  2. set SQL_Str=' truncate #table#_archive' +
  3. ' insert #table#_archive' +
  4. ' select * from #table#' +
  5. ' truncate #table#'
  6.  
  7. exec replace(SQL_Str, '#table#, 'products')
  8. exec replace(SQL_Str, '#table#, 'customers')
  9.  
this does reply on your archive tables being the same name as the main table with _archive appended
Feb 18 '11 #2
Thank you.
Feb 21 '11 #3

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

Similar topics

7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
2
by: Mark | last post by:
I created a test to check the execution time difference between executing a SQL Server stored procedured using explicit parameters versus not. In one case I created new SqlParameters in the code,...
4
by: Tim::.. | last post by:
Can someone tell me a better way or give me a link that shows a better way to create large numbers of SQL parameters... Example... A better way to write this code! <code> Sub...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
2
by: Hexman | last post by:
Hello All, Well I'm stumped once more. Need some help. Writing a simple select and update program using VB.Net 2005 and an Access DB. I'm using parameters in my update statement and when trying...
12
by: pamelafluente | last post by:
Hi guys, In the past I have used several time optional parameters in my function. But Now I am more inclined to think that they are more dangerous than useful, and probably better to be...
1
by: John Kotuby | last post by:
Hi all, I am working on porting an application from VB6 to VB.NET 2003 and am running into some problems. When declaring and populating the parameters for a SQL Stored Procedure by using the...
0
by: Xah Lee | last post by:
In this article, i explain how the use of bit masks is a hack in many imperative languages. Often, a function will need to take many True/False parameters. For example, suppose i have a function...
2
by: Jared Grant | last post by:
I am trying to find the value from some output parameters from a stored procedure. I have tried several different methods but somehow cannot get it to work. here is my source code: dim dr as...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.