Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with Insert Query

Posted on 2011-03-25
5
Medium Priority
?
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 60

Accepted Solution

by:
HainKurt earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 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