Solved

Help with Insert Query

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
View SQL 2005 Job package 16 70
Sql server function help 15 37
Downgrading MS SQL 2008 R2 Enterprise to MS SQL 2005 Standard? 12 65
Stored Proc - Rewrite 42 59
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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