I am newbie in SQL Server 2008.There are three tables which I am using. I have created two Proc for my result.But I want that these two Proc combined in single Proc.
Below is my proc –
First Proc –
CREATE PROC [GetPaymentGateway]
@CompanyID VARCHAR(3),
@CCMSalesChannel VARCHAR(50)
AS
declare @mainquery varchar(max)
set @mainquery='SELECT credit_card_master.card_name, credit_card_master.card_type,'+ @CCMSalesChannel +' FROM credit_card_master
INNER JOIN PaymentGateway_master
ON PaymentGateway_master.payment_gateway_code = credit_card_master.payment_gateway_code
WHERE [company_id] = '''+@CompanyID+''''
exec (@mainquery)
Second Proc –
CREATE PROC [GetPaymentGateway2]
@CompanyID VARCHAR(3),
@NBSalesChannel VARCHAR(50)
AS
declare @mainquery varchar(max)
set @mainquery='SELECT PG_NetBanking_Charges.Online_DC_Charge_Amt, PaymentGateway_master.Payment_Gateway_Name,'+ @NBSalesChannel +' FROM PG_NetBanking_Charges
INNER JOIN PaymentGateway_master
ON PaymentGateway_master.payment_gateway_code = PG_NetBanking_Charges.payment_gateway_code
WHERE [company_id] = '''+@CompanyID+''''
exec (@mainquery)
Please suggest me.Is it possible with hash table or etc.
Thanks in advance.
EDIT – I wanted these two results in single result(like in single table)
EDIT – There are five columns in my table but which one I will select, it will get to know on run time based on param.
You should avoid doing that kind of SQL inside stored procs unless its absolutely necessary and you are 1000% sure that you are sanitizing your input properly. The fact that all input params are varchar and these seem to be stored procs used to retrieve payment information, is disturbing, to say the least. You are still vulnerable to SQL Injection attacks by doing that kind of dynamic sql inside procs.
Both procs can be rewritten as follows in one single proc:
or perhaps you want this:
Update In general, one can select a particular column based on a parameter at “run-time”, as follows: