2016-12-28 56 views
0

我是SQL Server的新手,想實現這個場景。我的存儲過程從C#Web應用程序獲取8個輸入參數,並檢查表中的所有輸入。爲此目的,我寫了這個簡單的存儲過程:如何在我的存儲過程中實現這個T-SQL?

CREATE PROCEDURE CheckValid 
    @p_bank varchar, 
    @p_pay_date varchar, 
    @p_bill_id varchar, 
    @p_payment_id varchar, 
    @p_ref_code varchar, 
    @p_branch varchar, 
    @p_channel_type varchar, 
    @p_send_date varchar 
AS 
BEGIN 
    SELECT 
     [p_bank], [p_pay_date], [p_bill_id], [p_payment_id], 
     [p_ref_code], [p_branch], [p_channel_type], [p_send_date] 
    FROM 
     [SAMPLE].[dbo].[MixedTable] 
    WHERE 
     [p_bank] = @p_bank 
     AND [p_pay_date] = @p_pay_date 
     AND [p_bill_id] = @p_bill_id 
     AND [p_payment_id] = @p_payment_id 
     AND [p_ref_code] = @p_ref_code 
     AND [p_branch] = @p_branch 
     AND [p_channel_type] = @p_channel_type 
     AND [p_send_date] = @p_send_date 
END 

但要恢復到C#應用程序這種情況下,例如C#將所有領域,但是當用於此目的的存儲過程中選擇運行無法找到數據,例如p_bill_id不正確的插入表中選擇查詢的詳細解釋到哪裏在[p_bill_id]子句= @ p_bill_id不信任,現在想回SP這樣的:

p_bill_id,not found 

和其他例如C#中的所有變量正確的,但兩場[ p_channel_type]和[p_payment_id]不正確到where子句,但其他6字段現在正確SP返回th S:

[p_channel_type],not found 
[p_payment_id],not found 

問題的總結:

如果沒有找到傳遞的參數值數據,我希望它返回相應的列。

例如:

[p_channel_type],not found 
[p_payment_id],not found 
+2

[不良習慣踢:宣佈不VARCHAR(長度)](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/ 10/09/bad-habits-to-kick-declaring-varchar-without-length.aspx) - 你應該總是**爲任何'varchar'變量和參數提供一個長度,你使用 –

回答

1

注意,varchar意味着varchar(1)所以你應該明確地爲每一個參數指定長度一樣varchar(100)

CREATE PROCEDURE CheckValid 
    @p_bank varchar(<length>), 
    @p_pay_date varchar(<length>), 
    @p_bill_id varchar(<length>), 
    @p_payment_id varchar(<length>), 
    @p_ref_code varchar(<length>), 
    @p_branch varchar(<length>), 
    @p_channel_type varchar(<length>), 
    @p_send_date varchar(<length>) 
AS 
BEGIN 
    if not exists(select 1 from dbo.BankTable where p_bank = @p_bank) 
    begin 
     raiserror('Bank %s not found', 16, 1, @p_bank) 
     return 
    end 

    if not exists(select 1 from dbo.BillTable where p_bill_id = @p_bill_id) 
    begin 
     raiserror('Bill %s not found', 16, 1, @p_bill_id) 
     return 
    end 

    ... 

    SELECT [p_bank],[p_pay_date],[p_bill_id],[p_payment_id],[p_ref_code],[p_branch],[p_channel_type],[p_send_date] 
    FROM [SAMPLE].[dbo].[MixedTable] 
    where [p_bank][email protected]_bank and [p_pay_date][email protected]_pay_date 
     and [p_bill_id][email protected]_bill_id and [p_payment_id][email protected]_payment_id 
     and [p_ref_code][email protected]_ref_code and [p_branch][email protected]_branch 
     and [p_channel_type][email protected]_channel_type and [p_send_date][email protected]_send_date 

END 
GO 
+0

請等待測試 –

+0

運行時出現此錯誤:消息50000,級別16,狀態1,過程CheckValidbehzad,行18 [批處理開始行2] 未找到行1 –

0

而不是對這一舉動「驗證」的邏輯來創建存儲過程你的C#應用​​程序。
數據庫只是IO設備,我認爲在IO設備中保留「業務邏輯」不是一個好方法。

// Class which represent your eight parameters 
public class Data 
{ 
    public string Bank { get; set; } 
    public string PayDate { get; set; } 
    public string BillId { get; set; } 
    public string PaymentId { get; set; } 
    public string RefCode { get; set; } 
    public string Branch { get; set; } 
    public string ChannelType { get; set; } 
    public string SendDate { get; set; } 
} 

public class Validation 
{ 
    private Data _data; 

    public Validation(Data data) 
    { 
     _data = data; 
    } 

    public IEnumerable<string> Validate() 
    { 
     var columns = new KeyValuePair<string, string>[] 
     { 
      new KeyValuePair("p_bank", _data.Bank), 
      new KeyValuePair("p_pay_date", _data.PayDate), 
      new KeyValuePair("p_bill_id", _data.BillId), 
      new KeyValuePair("p_payment_id", _data.PaymentId), 
      new KeyValuePair("p_ref_code], _data.RefCode), 
      new KeyValuePair("p_branch", _data.Branch), 
      new KeyValuePair("p_channel_type", _data.ChannelType), 
      new KeyValuePair("p_send_date", _data.SendDate) 
     }; 

     return columns.Where(pair => IsValueExists(pair.Key, pair.Value) == false); 
    } 

    private bool IsValueExists(string columnName, string value) 
    { 
     var query = 
      $"SELECT [{columnName}] 
       FROM [SAMPLE].[dbo].[MixedTable] 
       WHERE [{columnName}] = @value"; 

     var parameter = new SqlParameter 
     { 
      ParameterName = "@value", 
      SqlDbType = SqlDbType.VarChar, 
      Value = _data.Bank 
     }; 

     using (var connection = new SqlConnection(yourConnectionString)) 
     using (var command = new SqlCommand(query, connection)) 
     { 
      command.Parameters.Add(parameter); 
      connection.Open(); 
      var value = command.ExecuteScalar(); 
      return value != null; // null returned if no rows exists 
     } 
    } 
} 

然後你就可以使用這種方法的地方

var data = new Data { Bank = "BankName", RefCode = "SomeRefcode" } // put all values 
var validation = new Validation(data); 
var invalidValues = validation.Validate(); 

foreach(var invalidValue in invalidValues) 
{ 
    // Print or save column names where value is invalid 
}