Solved

Help with Insert Query

Posted on 2011-03-25
5
261 Views
Last Modified: 2012-05-11
Hi Experts

I need to insert a new row to a table which contains 99% of the information from an existing row but with a few minor changes. I pass the existing unique ID of the row I wish to copy and the new info I need to insert. I'm struggling a little with the syntax and I'm not sure if I'm approaching it wrong.
A summary is below:
Create Procedure AddAnExtraLine()
	@Unique_ID [uniqueidentifier],
	@MyNewBatch [nvarchar](20)
BEGIN

	DECLARE @NEWUnique_ID [uniqueidentifier]
	SELECT @NEWUnique_ID = NewId()

	INSERT INTO [Mytable]
		([Unique_ID]
		,[UniqueID2]
		,[MyDate]
		,[Pmt_Type]
		,[Pmt_Ref]
		,[Description]
		,[Quantity]
		,[UnitPrice]
		,[NettAmt]
		,[VatAmt]
		,[Batch_ID])
	 VALUES
		(SELECT (@NEWUnique_ID 
		,[UniqueID2]
		,[MyDate]
		,[Pmt_Type]
		,[Pmt_Ref]
		,[Description]
		,0
		,0
		,0
		,0
		,@MyNewBatch)
	FROM [Mytable] 
	WHERE [Unique_ID] = @Unique_ID)
END

Open in new window

0
Comment
Question by:alexking
5 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35214485
try this

Create Procedure AddAnExtraLine()
      @Unique_ID [uniqueidentifier],
      @MyNewBatch [nvarchar](20)
BEGIN

      DECLARE @NEWUnique_ID [uniqueidentifier]
      SELECT @NEWUnique_ID = NewId()

      INSERT INTO [Mytable]
            ([Unique_ID]
            ,[UniqueID2]
            ,[MyDate]
            ,[Pmt_Type]
            ,[Pmt_Ref]
            ,[Description]
            ,[Quantity]
            ,[UnitPrice]
            ,[NettAmt]
            ,[VatAmt]
            ,[Batch_ID])
       VALUES
            Select (@NEWUnique_ID
            ,[UniqueID2]
            ,[MyDate]
            ,[Pmt_Type]
            ,[Pmt_Ref]
            ,[Description]
            ,0
            ,0
            ,0
            ,0
            ,@MyNewBatch)
      FROM [Mytable]
      WHERE [Unique_ID] = @Unique_ID)
END
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35214490
sorry try thsi

Create Procedure AddAnExtraLine()
      @Unique_ID [uniqueidentifier],
      @MyNewBatch [nvarchar](20)
BEGIN

      DECLARE @NEWUnique_ID [uniqueidentifier]
      SELECT @NEWUnique_ID = NewId()

      INSERT INTO [Mytable]
            ([Unique_ID]
            ,[UniqueID2]
            ,[MyDate]
            ,[Pmt_Type]
            ,[Pmt_Ref]
            ,[Description]
            ,[Quantity]
            ,[UnitPrice]
            ,[NettAmt]
            ,[VatAmt]
            ,[Batch_ID])
       VALUES
            (Select @NEWUnique_ID
            ,[UniqueID2]
            ,[MyDate]
            ,[Pmt_Type]
            ,[Pmt_Ref]
            ,[Description]
            ,0
            ,0
            ,0
            ,0
            ,@MyNewBatch
      FROM [Mytable]
      WHERE [Unique_ID] = @Unique_ID)
END
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35214506
Create Procedure AddAnExtraLine()
      @Unique_ID [uniqueidentifier],
      @MyNewBatch [nvarchar](20)
BEGIN

      DECLARE @NEWUnique_ID [uniqueidentifier]
      SELECT @NEWUnique_ID = NewId()

      INSERT INTO [Mytable]
            ([Unique_ID]
            ,[UniqueID2]
            ,[MyDate]
            ,[Pmt_Type]
            ,[Pmt_Ref]
            ,[Description]
            ,[Quantity]
            ,[UnitPrice]
            ,[NettAmt]
            ,[VatAmt]
            ,[Batch_ID])
       VALUES
            (SELECT @NEWUnique_ID
            ,[UniqueID2]
            ,[MyDate]
            ,[Pmt_Type]
            ,[Pmt_Ref]
            ,[Description]
            ,0
            ,0
            ,0
            ,0
            ,@MyNewBatch
      FROM [Mytable]
      WHERE [Unique_ID] = @Unique_ID)
END
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 35214717
I guess we dont need "VALUES"
try this:
Create Procedure AddAnExtraLine()
      @Unique_ID [uniqueidentifier],
      @MyNewBatch [nvarchar](20)
BEGIN
      DECLARE @NEWUnique_ID [uniqueidentifier]
      SELECT @NEWUnique_ID = NewId()

      INSERT INTO [Mytable]
            ([Unique_ID]
            ,[UniqueID2]
            ,[MyDate]
            ,[Pmt_Type]
            ,[Pmt_Ref]
            ,[Description]
            ,[Quantity]
            ,[UnitPrice]
            ,[NettAmt]
            ,[VatAmt]
            ,[Batch_ID])
            (SELECT @NEWUnique_ID 
            ,[UniqueID2]
            ,[MyDate]
            ,[Pmt_Type]
            ,[Pmt_Ref]
            ,[Description]
            ,0
            ,0
            ,0
            ,0
            ,@MyNewBatch
      FROM [Mytable] 
      WHERE [Unique_ID] = @Unique_ID)
END

Open in new window

0
 

Author Closing Comment

by:alexking
ID: 35214744
Aaah Thankyou!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now