[英]How to select output scalar values in sql server?
嗨,我仍然是TSQL的新手。 如何輸出標量變量以便我的vb代碼可以訪問它?
在VB中我使用rs方法。 在這種情況下,我將創建3 rs以便能夠訪問下面的數據。 我想有一個存儲過程可以給我4個值,我需要不使用多個rs。
Create PROCEDURE [dbo].[sp_tblTransaction_GET_All_Totals]
@TransID bigint
AS
Declare @MyTotalCharges as money
Declare @MyTotalDiscounts as money
Declare @MyTotalPayments as money
Declare @TotalCharges as money
Declare @TotalDiscounts as money
Declare @TotalPayments as money
Declare @Balance as money
SELECT @MyTotalCharges = SUM(Amount)
FROM tblTransactionDetails
WHERE (TransID = @TransID)
SELECT @MyTotalDiscounts = SUM(Amount)
FROM tblTransaction_DP
WHERE (TransID = @TransID)
SELECT @MyTotalPayments = SUM(Amount)
FROM tblPayments
WHERE (TransID = @TransID)
--Below are the scalar values I need to be ouputed and accessed by my vb app.
--How can I output the values below?
@TotalCharges = @MyTotalCharges
@TotalDiscounts = @MyTotalDiscounts
@TotalPayments = @MyTotalPayments
@Balance = (@MyTotalCharges - @MyTotalDiscounts - @MyTotalPayments)
你有沒有嘗試過?
SELECT @Balance AS 'Balance', @TotalCharges AS 'TotalCharges' ...
您需要將存儲過程中的值作為表返回。 將此添加到您的過程中。
SELECT
@TotalCharges as [Total Charges],
@TotalDiscounts as [Total Discounts],
@TotalPayments as [TotalPayments],
@Balance as [Balance]
然后,您可以從VB應用程序執行存儲過程並將表加載到DataTable中。
int transactionID = 0;
DataTable table = new DataTable();
using (var connection = new SqlConnection("connectionString")
using (var command = new SqlCommand("sp_tblTransaction_GET_All_Totals", connection)
{
connection.Open();
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@TransID", transactionID);
using (var adapter = new SqlDataAdapter(command))
{
adapter.Fill(table);
}
}
這是關於SqlDataAdapter的文檔,它將包括C#和VB中的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.