有一個在上的數據是如何提供的參數或查詢所以這裏有一些選擇上下文中的問題的資料很少。
創建存儲過程,並調用它多次:
-- Stored procedure
create procedure dbo.GetSalesByCompany
@companyName varchar(25)
AS
SET NOCOUNT ON;
Select * from sales where Company_Name = @company1;
--Usage
exec dbo.GetSalesByCompany 'AA';
exec dbo.GetSalesByCompany 'BB';
-- Usage with params
DECLARE @company1 varchar(25)
set @company1 = 'AA';
exec dbo.GetSalesByCompany @company1;
set @company1 = 'BB';
exec dbo.GetSalesByCompany @company1;
創建用戶定義的表值函數。這可以從存儲過程中如果需要的話可以稱爲:
-- User defined table-valued function
Create Function dbo.udf_GetSalesByCompany
(@companyName varchar(25))
RETURNS TABLE AS
RETURN Select * from sales where Company_Name = @company1;
--Usage
SELECT * FROM dbo.udf_GetSalesByCompany('AA');
SELECT * FROM dbo.udf_GetSalesByCompany('BB');
-- Usage with params
DECLARE @company1 varchar(25)
set @company1 = 'AA';
SELECT * FROM dbo.udf_GetSalesByCompany(@company1);
set @company1 = 'BB';
SELECT * FROM dbo.udf_GetSalesByCompany(@company1);
或者你的「IN」的語句去調用一次查詢,多個過濾器值,在這種情況下,公司名稱。
--Use and "IN" statement
Select * from sales where Company_Name IN('AA', 'BB')
參數如何填充不同的值?你能展示你的整個過程嗎? –