Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Link more than one item for one selling operation ... Expand / Collapse
Author
Message
Posted Friday, May 23, 2014 9:19 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 23, 2014 4:59 PM
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
Post #1574090
Posted Friday, May 23, 2014 9:31 AM This worked for the OP Answer marked as solution


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, July 25, 2014 3:21 PM
Points: 13,083, Visits: 11,918
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 Moden's 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)
Post #1574099
Posted Friday, May 23, 2014 2:09 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 23, 2014 4:59 PM
Points: 10, Visits: 20
.
Post #1574230
Posted Friday, May 23, 2014 2:15 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 23, 2014 4:59 PM
Points: 10, Visits: 20
.
Post #1574231
Posted Friday, May 23, 2014 2:41 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 23, 2014 4:59 PM
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!
Post #1574242
Posted Friday, May 23, 2014 2:57 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, July 25, 2014 3:21 PM
Points: 13,083, Visits: 11,918
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 Moden's 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)
Post #1574248
Posted Friday, May 23, 2014 3:09 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, May 23, 2014 4:59 PM
Points: 10, Visits: 20
Ok , thank you , I will
Post #1574251
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse