sql parameters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devquest
    New Member
    • Dec 2009
    • 12

    #1

    sql parameters

    I want to truncate a products_archiv e table and insert the products table into the products_archiv e 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.
  • gpl
    New Member
    • Jul 2007
    • 152

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

    Code:
    declare SQL_str varchar(2048)
    set SQL_Str='Select * from products'
    exec SQL_Str
    you see yhat you could build a simple string and then execute it with a different name, for example
    Code:
    declare SQL_str varchar(2048)
    set SQL_Str=' truncate #table#_archive' +
    ' insert #table#_archive' +
    ' select * from #table#' +
    ' truncate #table#'
    
    exec replace(SQL_Str, '#table#, 'products')
    exec replace(SQL_Str, '#table#, 'customers')
    this does reply on your archive tables being the same name as the main table with _archive appended

    Comment

    • devquest
      New Member
      • Dec 2009
      • 12

      #3
      Thank you.

      Comment

      Working...