Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2003, 08:24 AM
Registered User
 
Join Date: Sep 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Insert into different Tables

Hi Wondering if anyone can help. My problem is this

I have numerous tables in my database all of which are exactly the same format, just different names. I am trying to write a stored procedure that will allow me to choose which table I insert details into based on a variable. I'm currently trying to do this with a case statement without much success. This stored proc is being run by a .net web application.I have to do this with a stored proc, and cannot generate the SQL directly from the application (due to the specifications of the application)

the basic flow of what I have is below

Variable called @Table of type int passed to SProc along with all values

    BEGIN
                INSERT INTO

             CASE @Table
            WHEN 1 THEN dbo.cAlerts
            WHEN 2 THEN dbo.cAnalysisCode
             END
            (
            type,
            IdAlpha,
            [Description])
        VALUES
            (
            @Type,
            @Alpha,
            @Description)

    END

Any help or suggestions would be appreciated

Thanks

Carl

 
Old September 16th, 2003, 03:17 AM
Authorized User
 
Join Date: Jun 2003
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

If you know when 1 is passed it needs to insert to cAlerts and when 2 is passed to insert to cAnalysisCode etc. You can use separate INSERT statements in your SP :-

IF @Table = 1
BEGIN
    INSERT INTO dbo.cAlerts
    (type, IdAlpha, [Description])
        VALUES (@Type, @Alpha, @Description)
END

IF @Table = 2
BEGIN
    INSERT INTO dbo.cAnalysisCode
    (type, IdAlpha, [Description])
        VALUES (@Type, @Alpha, @Description)
END

Nickie
 
Old September 16th, 2003, 03:52 AM
Registered User
 
Join Date: Sep 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, thanks. I was thinking about using a similar solution, and in the interests of getting the project working, I will. Will there be an issue of running very long scripts on SQL server, even though only a few lines of that script are executed, since there are around 30 tables like this, and I have to effectively re-write the insert for each one

Thanks again

Carl






Similar Threads
Thread Thread Starter Forum Replies Last Post
how Insert record into two tables mfarooqw ASP.NET 1.0 and 1.1 Basics 2 July 10th, 2007 05:38 PM
insert data in two tables from form mfarooqw ASP.NET 1.0 and 1.1 Professional 1 July 10th, 2007 08:34 AM
Insert Data into two tables Lofa PHP Databases 4 July 7th, 2007 10:36 PM
Insert record Into 2 related tables at once kalchev ASP.NET 2.0 Basics 2 May 9th, 2006 05:10 AM
Insert into Multiple Tables bmalex1 Beginning PHP 0 February 6th, 2006 12:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.