Stored procedure nightmare

Results 1 to 2 of 2

Thread: Stored procedure nightmare

  1. #1
    Join Date
    Dec 1969

    Default Stored procedure nightmare

    I&#039m trying to do something which I assume is very simple with a stored procedure. This is an example <BR><BR>pulled of the 4Guys web site to illustrate what I need to do:<BR><BR>CREATE PROCEDURE sp_myInsert<BR> @FirstName varchar(20),<BR> @LastName varchar(30),<BR> @MyTableParam varchar(20)<BR>As<BR>INSERT INTO TableName(FirstName, LastName)<BR>values(@FirstName, @LastName)<BR>Go<BR><BR>Now this stored procedure is simple enough, even to me. However, rather than insert the values of <BR><BR>FirstName, and LastName into the table called &#039TableName&#039 I need to insert them into the table <BR><BR>specified in the parameter @MyTableParam. I have tried doing the following (the obvious) but it <BR><BR>generates a syntax error.<BR><BR>CREATE PROCEDURE sp_myInsert<BR> @FirstName varchar(20),<BR> @LastName varchar(30),<BR> @MyTableParam varchar(20)<BR>As<BR>INSERT INTO @MyTableParam(FirstName, LastName)<BR>values(@FirstName, @LastName)<BR>Go<BR><BR>Just out of interest, it&#039s actually a SELECT statement that I need to use this format in.

  2. #2
    Leungas Guest

    Default RE: Stored procedure nightmare

    I don&#039t think you can actually do that, however, you can use a if-else condition within the store procedure to determine which insert to execute...<BR><BR>e.g.(note: My syntax is in sql 7.0)<BR>alter procedure sp_InsertStatements<BR>( @FirstName varchar(40),<BR> @LastName varchar(40),<BR> @TableNumber int )<BR><BR>if @TableNumber = 0<BR>begin<BR> -- Insert Statement for Table 0<BR>end<BR><BR>if @TableNumber = 1<BR>begin<BR> -- Insert Statement for Table 1<BR>end<BR><BR>etc...<BR><BR>I hope this helps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts