Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


insert and update problem in stored proc


insert and update problem in stored proc

Author
Message
johndewitte
johndewitte
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 11
I want to insert values from a select query into table "Delkk"

The select query
has the following result:

Orderdatum txtKortingBdr Klantnummer Tal

2010-09-07 13:55:55.000 14,1305 1445 5
2010-09-07 16:58:41.000 5,695 2128 1


Delkk has the same columns:

Orderdatum, Tal, txtKortingBdr,Klantnummer.


When I try to execute the stored proc "DelKK2jaar"
I get the following message in SSMS:

"A nested INSERT, UPDATE, DELETE, or MERGE statement must have an OUTPUT clause."


The stored proc :

ALTER PROCEDURE [dbo].[DelKK2jaar]

AS
BEGIN

SET NOCOUNT ON;

DELETE
A
FROM
klantenkaart AS A
INNER JOIN
(
INSERT
Delkk
(
Orderdatum,
txtKortingBdr,
Klantnummer,
Tal
)
OUTPUT Inserted.* into delkk
( Orderdatum, txtKortingBdr,Klantnummer, Tal)

select
Max(klantenkaart.Orderdatum) AS Orderdatum,
txtKortingBdr,
Klant.Klantnummer,
Count(Klant.txtKortingBdr) AS Tal

FROM
Gemeente
INNER JOIN
Klant
ON Gemeente.GemeenteId = Klant.GemeenteId
INNER JOIN
klantenkaart
ON Klant.Klantnummer = klantenkaart.Klantnummer
GROUP BY
Klant.Klantnummer,
Klant.Naamvoornaam,
Klant.txtKortingBdr,
Gemeente.Gemeente
HAVING
Max(klantenkaart.Orderdatum) < DATEADD(YEAR, -2, SYSDATETIME())
) AS B
ON A.klantnummer = B.Klantnummer

END



Can somebody have a look and help me?

Txs,

John
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5778 Visits: 7133
In your delete statement, you have an INNER JOIN to an INSERT statement, where the INSERT does not have an OUTPUT clause.
This is the cause of the given error.

What is it exactly you are attempting to achieve with this procedure?

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5778 Visits: 7133
Alternatively, in the
OUTPUT Inserted.* into delkk
( Orderdatum, txtKortingBdr,Klantnummer, Tal)

line, remove the " into delkk" statement, i.e. the OUTPUT statement should reflect
OUTPUT Inserted.*



____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
OTF
OTF
Old Hand
Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)Old Hand (316 reputation)

Group: General Forum Members
Points: 316 Visits: 4128
Stewart "Arturius" Campbell (9/10/2012)
Alternatively, in the
OUTPUT Inserted.* into delkk
( Orderdatum, txtKortingBdr,Klantnummer, Tal)

line, remove the " into delkk" statement, i.e. the OUTPUT statement should reflect
OUTPUT Inserted.*



That's what I thought initially, however, if you try to run the code with the modification suggested it
presents the following error:

Msg 10727, Level 15, State 1, Procedure DelKK2jaar, Line 17
A nested INSERT, UPDATE, DELETE, or MERGE statement is not allowed on either side of a JOIN or APPLY operator

I don't think the way the OP has written the code is valid Sql in any case.

I think in order to do what he wants to do he would have to:

Run the nested insert with the output clause into a #temp table or variable
Do the Join on that to perform any other actions.
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5778 Visits: 7133
OTF (9/10/2012)
I don't think the way the OP has written the code is valid Sql in any case.


I'm inclined to agree, which is why i initially asked what the actual requirement is.

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search