behzad razzaqi
behzad razzaqi

Reputation: 147

How can I implement this T-SQL in my stored procedure?

I'm new to SQL Server and want to implement this scenario. My stored procedure gets 8 input parameters from a C# web application, and checks all input has into the table. For that purpose I wrote this simple stored procedure:

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

But want to return to c# application this scenario, for example c# sends all field but when stored procedure select run for this purpose can not find data, for example p_bill_id not correct into the table for more explain in select query into where clause in the [p_bill_id]=@p_bill_id not trust and now want to return sp this :

p_bill_id,not found

and other example c# all variable correct but two field [p_channel_type] and [p_payment_id] not correct into where clause but other 6 field correct now SP return this:

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

Summary of question:

When data for passed parameter value is not found, I want it to return that corresponding column.

For example:

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

Upvotes: 0

Views: 55

Answers (2)

Fabio
Fabio

Reputation: 32445

Instead of creating stored procedure for this move "validation" logic to your c# application.
Database is just IO device and I think keeping "business logic" in IO device not a good approach.

// 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
        }
    }
}

Then you can use this method somewhere

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
}

Upvotes: 0

IVNSTN
IVNSTN

Reputation: 9299

Note, varchar means varchar(1) so you should specify length for each argument explicitly like 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]=@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
GO

Upvotes: 1

Related Questions