2016-12-05 109 views
-4

我是一名測試人員,我必須運行大量的選擇查詢來篩選出我正在測試的信息。如何在SQL中定義和使用全局變量

enter image description here

有沒有一種方法,使全局變量,用它來代替粘貼相同的值嗎?

enter image description here

示例代碼:

Select l.prod_package,m.* from avtt7m0 m, avtt7l0 l 
where l.cust_id='52317162090004' 
and l.ar_id=m.ar_id and m.lc_st_code='ACT'; 

Select m.* from avtt7m0 m, avtt7l0 l 
where l.cust_id='52317162090004' 
and l.ar_id=m.ar_id and m.lc_st_code='ACT'; 

Select * From AKTTD90 
where cust_id in ('52317162090004'); 

Select * From Kndt7m0 
where cust_id in ('52317162090004'); 
+0

您可以使用t-sql變量並事先進行設置。如果您發佈的是您使用的代碼而不是圖片,我將向您展示如何使用它 –

+2

您正在使用哪種dbms。 (答案可能是產品特定的。) – jarlh

+0

我正在使用SQL開發人員。這裏是代碼:從avtt7m0 m,avtt7l0 l選擇l.prod_package,m。*,其中l.cust_id ='52317162090004'和l.ar_id = m.ar_id和m.lc_st_code ='ACT'; 從avtt7m0 m,avtt7l0 l選擇m。*其中l.cust_id ='52317162090004'和l.ar_id = m.ar_id和m.lc_st_code ='ACT'; 選擇* From AKTTD90 where cust_id in('52317162090004'); 選擇*從Kndt7m0 where cust_id in('52317162090004'); – VItas

回答

0

你可以聲明變量併爲其分配customer值。

DECLARE @Customer BIGINT='52371762090004' 

SELECT * FROM AKTTD90 WHERE [email protected] 

UPDATE:嘗試執行上述查詢。

+0

它不工作。我使用SQL開發4.1可以打賭原因?我不太瞭解SQL查詢。所以可能是因爲我沒有給你所有的信息來幫助我。但如果可以的話,我會盡力回答。 – VItas

+0

@VItas我已經更新了答案。做檢查。 –

+0

它給出了一個錯誤。你知道什麼是錯的嗎?誤差在命令開始位於第1行: DECLARE @Customer BIGINT = '52371762090004' SELECT * FROM KNDT7L0 WHERE的cust_id = @客戶 錯誤報告: ORA-06550:第1行,第9欄: PLS-00103:出現符號「@」在需要下列之一時: 開始功能編譯程序亞型類型<標識符>當前光標刪除 存在現有 06550. 00000 - 「行%S,柱% s:\ n%s「 *原因:通常是PL/SQL編譯錯誤。 *操作: – VItas

0

這針對SQL Server - 你的問題沒有一個特定的RDMS。您可以Declare變量(必須與@開始,然後Set它然後使用它(在大多數情況下):

SET @CustomerID = 12345 
SELECT * FROM dbo.Customers WHERE CustomerID = @CustomerID 

注意,這不能用於某些東西,如一個表名稱的變量也。請注意,這些變量不是在這個意義上實際上全球,他們將無法生存一個GO聲明

+0

看來SQL開發人員4.1不喜歡這種語法。如果我沒有提供所有信息,Sory。這只是因爲我是SQL新手,所以我不知道要提及什麼是重要的。爲mysql/sql開發人員編輯 – VItas

+0

。這應該現在工作(聲明部分不需要爲MySQL)。 – jleach

0

一個選擇就是使用PL/SQL的包變量:

create or replace package steptest as 
    procedure set(a number); 
    function get return number; 
end; 
/

create or replace package body steptest as 
    x number; 
    procedure set(a number) is 
    begin 
    x:=a; 
    end; 
    function get return number is 
    begin 
    return x; 
    end; 
begin 
    x:=0; 
end;  
/

和TH您可以:

exec steptest.set(1234); 

select steptest.get from dual;