2017-01-20 23 views
0

我有表:如何在選擇查詢中使用參數?

表名:tbl_Income

EmployeeID  Element  FinancialYear Jan Feb Mar 

00402060   Basic   2016-2017  100 200 300 
00402060   HRA   2016-2017  100 200 300 
00402060   DA   2016-2017  100 200 300 

中,我想從tbl_Income獲取數據。

其中我提取下面的問題。

Declare @Month varchar(10) = 'Jan' 

select @Month from tbl_Income where EmployeeID = '00402060' and Element = 'Basic' and FinancialYear = '2016-2017' 

我想下面的輸出

OUTPUT : 

    Jan 
1 100  

請幫我...

+0

您必須使用動態SQL執行此操作。順便說一下,這不是一個很好的數據庫設計。 – HoneyBadger

+0

您可以使用大小寫聲明來實現此目的。 – TechEnthusiast

回答

2

您可以使用動態SQL這樣的:

declare @sql nvarchar(max) 
Declare @Month varchar(10) = 'Jan' 
declare @income int 

set @sql = 'select @inc=' + @Month + ' from tbl_Income where EmployeeID = ''00402060'' and Element = ''Basic'' and FinancialYear = ''2016-2017''' 

exec sp_executesql @sql, N'@inc int OUTPUT', @[email protected] OUTPUT 
select @income as income 

要小心的是,這是開放的到SQL注入攻擊。

你最好修復你的設計。

+0

如何將此輸出'100'值存儲在變量中?請幫助我.. @ GurV –

+0

可以檢查我的答案我存儲在一個變量的值.. @ DhavalPurohit – Chanukya

+0

@Chanukya - 你的答案是完全一樣的我的。Dhaval - 看看[這個](http://stackoverflow.com/questions/3840730/getting-result-of-dynamic-sql-into-a-variable-for-sql-server) – GurV

1
Declare @Month varchar(10) = 'Jan' 
declare @v nvarchar(max) 
declare @v1 INT 
set @v =CONCAT('select @v1=' ,@month, ' from 
table_a where EmployeeID = ''00402060'' and Element = ''Basic'' and FinancialYear = ''2016-2017''') 
PRINT @V 
EXECUTE sp_executesql @v,N'@V1 INT OUTPUT', @[email protected] OUTPUT; 
SELECT @V1; 
0

[示例數據庫設計] [1]

[1]:https://i.stack.imgur.com/2vxEb.png - 表 「dbo.Month」

CREATE TABLE dbo.Month (
    Id int NOT NULL, 
    Name nvarchar(50) NOT NULL, 
    CONSTRAINT PK_Month PRIMARY KEY CLUSTERED (Id) 
) 

- 表IncomeType」

CREATE TABLE dbo.IncomeType (
    Id int NOT NULL, 
    Name nvarchar(50) NOT NULL, 
    CONSTRAINT PK_IncomeType PRIMARY KEY CLUSTERED (Id) 
) 

- 表「FinancialYear」

CREATE TABLE dbo.FinancialYear (
    Id int NOT NULL, 
    YearSpan nvarchar(50) NOT NULL, 
    CONSTRAINT PK_FinancialYear PRIMARY KEY CLUSTERED (Id) 
) 

- 表 「僱員」

CREATE TABLE dbo.Employee (
    Id int NOT NULL, 
    Name nvarchar(50) NOT NULL, 
    CONSTRAINT PK_Employee PRIMARY KEY CLUSTERED (Id) 
) 

- 表 「收入」

CREATE TABLE dbo.Income (
    Id int NOT NULL, 
    EmployeeId int NOT NULL, 
    TypeId int NOT NULL, 
    YearSpanId int NOT NULL, 
    MonthId int NOT NULL, 
    CONSTRAINT PK_Income PRIMARY KEY CLUSTERED (Id) 
) 

--Alter每個表中添加外鍵引用

ALTER TABLE Income 
    ADD CONSTRAINT FK_Income_Employee FOREIGN KEY (EmployeeId) REFERENCES Employee (Id) 

    ALTER TABLE dbo.Income 
    ADD CONSTRAINT FK_Income_FinancialYear FOREIGN KEY (YearSpanId) REFERENCES dbo.FinancialYear (Id) 


    ALTER TABLE dbo.Income 
    ADD CONSTRAINT FK_Income_IncomeType FOREIGN KEY (TypeId) REFERENCES dbo.IncomeType (Id) 

    ALTER TABLE dbo.Income 
    ADD CONSTRAINT FK_Income_Month FOREIGN KEY (MonthId) REFERENCES dbo.Month (Id) 
  1. 僱用eeID將來自Employee表
  2. 元素將
  3. FinancialYear --from財政年度表
  4. MonthId從月表

來自於收入型表現在一個簡單的加入可以幫助你使用外鍵引用獲取每個值