SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Link more than one item for one selling operation ...


Link more than one item for one selling operation ...

Author
Message
StarterProgrammer
StarterProgrammer
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 20
this my stored procedure and I'm sorry about names
ALTER proc [dbo].[storedP]
(@prdctName nvarchar(50)
,@ordrdQnty int
,@OrderID int
,@ordrPrice money
,@TrnDate datetime
,@TrnTotal money)

as
BEGIN TRY
begin transaction trs

DECLARE @TrnId int
SET @TrnId = 'SCOPE_IDENTITY'

INSERT INTO [dbo].[OrderProduct]
([OrdrId],
[prdctName]
,[ordrdQnty]
,[ordrPrice]
,[TrnId])
VALUES
(@OrderID,
@prdctName
,@ordrdQnty
,@ordrPrice
,@TrnId
)





INSERT INTO [dbo].[Transaction]
(
[OrdrId]
,[TrnDate]
,[TrnTotal])
VALUES
( @OrderId ,CURRENT_TIMESTAMP ,@TrnTotal)



commit tran
return 0
END TRY
BEGIN CATCH
rollback
return 1
END CATCH
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26317 Visits: 17553
StarterProgrammer (5/23/2014)
this my stored procedure and I'm sorry about names


No need to apologize about the names to me. The are painful but you are the one who has to suffer with them. ;-)

I think the main issue is you don't understand how IDENTITY works. How are you getting the value for @OrderID to call this procedure? This is the new value for OrdrId in OrderProduct right? That means since you are using IDENTITY you don't know it yet.

The next issue is you have declared a variable @TrnId as an int and are trying set the value to a string literal. I am quite confused by TrnID. What is that for? It seems like that is maybe the primary key in Transaction. Why is there a column in the parent table for that?

Something like this might be closer but your structures seems to be a bit out of alignment too.


ALTER proc [dbo].[storedP]
(
@prdctName nvarchar(50)
,@ordrdQnty int
--,@OrderID int
,@ordrPrice money
,@TrnDate datetime
,@TrnTotal money
) as

BEGIN TRY
begin transaction trs

INSERT INTO [dbo].[OrderProduct]
(
--[OrdrId], You don't want to insert an explicit value here
[prdctName]
,[ordrdQnty]
,[ordrPrice]
--,[TrnId]
)
VALUES
(
--@OrderID,
@prdctName
,@ordrdQnty
,@ordrPrice
--,@TrnId
)

INSERT INTO [dbo].[Transaction]
(
[OrdrId]
,[TrnDate]
,[TrnTotal]
)
VALUES
(
SCOPE_IDENTITY() ,
CURRENT_TIMESTAMP ,
@TrnTotal
)

commit tran
return 0
END TRY
BEGIN CATCH
rollback
return 1
END CATCH



You should check BOL and read about the IDENTITY property so you understand what it does and how it works.

http://msdn.microsoft.com/en-us/library/ms186775.aspx

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
StarterProgrammer
StarterProgrammer
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 20
.
StarterProgrammer
StarterProgrammer
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 20
.
StarterProgrammer
StarterProgrammer
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 20
It seems it works but it asked about the time which is CURRENT_TIMESTAMP , it supposed to get the current time of the system automatically ,right!
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26317 Visits: 17553
StarterProgrammer (5/23/2014)
It seems it works but it asked about the time which is CURRENT_TIMESTAMP , it supposed to get the current time of the system automatically ,right!


I think was a question. Here is the BOL entry. http://msdn.microsoft.com/en-us/library/ms188751.aspx

You might want to bookmark that. There is lots of incredibly valuable information there.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
StarterProgrammer
StarterProgrammer
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 20
Ok , thank you , I will :-)
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