2013-02-17 1 views
0

안녕하세요, 저는 여전히 TSQL을 처음 사용합니다. 내 vb 코드에서 액세스 할 수 있도록 Scalar 변수를 출력하려면 어떻게해야합니까?SQL Server에서 출력 스칼라 값을 선택하는 방법은 무엇입니까?

VB에서는 rs 메서드를 사용합니다. 이 경우 아래 데이터에 액세스하려면 3 rs를 만들어야합니다. 나는 여러 개의 rs를 사용하지 않고 필요한 4 개의 값을 줄 수있는 저장된 proc를 갖고 싶다.

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) 
+0

가능 중복 [T-SQL 저장 프로 시저의 선택된 값을 취득 (http://stackoverflow.com/questions/2881024/t-sql-get-selected-value-of-stored-procedure) – Kermit

답변

1

저장 프로 시저의 값을 테이블로 반환해야합니다. 이것을 프로 시저에 추가하십시오.

SELECT 
    @TotalCharges as [Total Charges], 
    @TotalDiscounts as [Total Discounts], 
    @TotalPayments as [TotalPayments], 
    @Balance as [Balance] 

VB 응용 프로그램에서 저장 프로 시저를 실행하고 테이블을 DataTable에로드 할 수 있습니다.

calling a stored procedure from C# using SqlDataAdapter

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); 
    } 
} 

다음은 모두 C# 및 VB에서 예제를 포함하여 SqlDataAdapter에 문서입니다.

MSDN - SqlDataAdapter Class (System.Data.SqlClient)

+0

니스! 대단히 감사합니다. @ Lo. 이것이 내가 필요한 것입니다. 완전한. – user2059064

1

시도해 보셨습니까?

SELECT @Balance AS 'Balance', @TotalCharges AS 'TotalCharges' ... 
관련 문제