感謝您的幫助傢伙輸出參數用於在同一個表中插入一行
我工作的一個基於Asp.net項目,我requiremnent是產生MaterialType +產品ID和的組合PARTNO - 4位數隨機數
注意:產品ID是主鍵,也將其設置爲輸出參數
例如如果材料的類型是500和產品ID生成55和隨機產生的無5434,然後部分沒有成爲555-5434
N嗷嗷我的問題是我怎麼能存儲在同一個表PARTNO,我有點想這樣
Connection.Open()
Dim trn As SqlClient.SqlTransaction
trn = Connection.BeginTransaction
Using trn
Dim sqlcode As New SqlParameter("@ProductID", Products.ProductID)
sqlcode.Direction = ParameterDirection.InputOutput
Using Command As New SqlCommand("Product_Write", Connection, trn)
Command.CommandType = CommandType.StoredProcedure
Command.Parameters.Add(sqlcode) Command.Parameters.AddWithValue("@MaterialType", Materialtype.MaterialTypeCode)
Command.Parameters.AddWithValue("@CategoryID", category.CategoryId)
Command.Parameters.AddWithValue("@ProductName", Products.ProductName)
Command.Parameters.AddWithValue("@ProductDescription", Products.ProductDescription)
Command.Parameters.AddWithValue("@ProductActive", Products.ProductActive)
Command.Parameters.AddWithValue("@ProductImage", Products.ProductImage)
Command.Parameters.AddWithValue("@PartNo", 0)
Command.ExecuteNonQuery()
Products.ProductID = CInt(sqlcode.Value)
'Insert the part no
Dim random As New Random()
Dim value As Integer = random.Next(9999)
Dim PartNo As String = CType((Materialtype.MaterialTypeCode + Products.ProductID).ToString + "-" + value.ToString, String)
'Dont know what to do
trn.Commit()
Connection.Close()
End Using
End Using
End Using
Return Products
End Function
ALTER PROCEDURE [dbo].[Product_Write]
@ProductID bigint OUTPUT,
@MaterialType int,
@CategoryID bigint,
@ProductName VARCHAR(MAX),
@ProductDescription VARCHAR(MAX),
@ProductActive Bit,
@ProductImage VARCHAR(MAX),
@PartNo VARCHAR(30)
AS
IF (@ProductID=0)
BEGIN
INSERT INTO T_Product(
MaterialType,
CategoryID,
ProductName,
ProductDescription,
ProductActive,
ProductImage,
PartNo)
VALUES(
@MaterialType,
@CategoryID,
@ProductName,
@ProductDescription,
@ProductActive,
@ProductImage,
@PartNo)
SET @ProductID=SCOPE_IDENTITY()
END
ELSE
UPDATE T_Product SET
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected],
[email protected]
WHERE [email protected]
Please help me out
感謝
您可以使用插入的表功能甚至scope_identity來完成存儲過程中的工作。 –
能否請你舉個例子 –
你需要創建一個新的更新查詢或存儲過程來更新PARTNO的值,比如Update Table Set PartNo = NewPartNo,其中ProductID = @ ProductId,然後再次調用ExecuteNonQuery – praveen