Solved

Help with Insert Query

Posted on 2011-03-25
5
263 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql query 8 48
SQL Server - Replace Carriage Returns for Excel Exports 17 49
Showing random records from database 10 35
Need help how to find where my error is in UFD 6 25
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

815 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

8 Experts available now in Live!

Get 1:1 Help Now