2012-04-19 44 views
1

我在創建SQL Anywhere中的動態sql語句時遇到問題。如何在SQL Anywhere中創建動態存儲過程?

CREATE PROCEDURE pplAnalysis 
AS 
BEGIN 

    DECLARE @Sql NVARCHAR(4000) 

    SELECT @Sql = "select * from cds.ppl" 

    EXECUTE(@Sql) 

END 

當我執行此過程中,我得到一個Column 'select * from cds.ppl' not found錯誤。

你能告訴我我做錯了什麼嗎?

+0

無論你寫在from必須是數據庫中的對象之後,表或視圖以及表/視圖名稱之間不能有一個點。什麼是cds.ppl? – 2012-04-19 18:14:18

回答

1

使用單引號。

SELECT @Sql = 'select * from cds.ppl' 
+0

我試過了,但它似乎不喜歡它與單引號。 – user1344736 2012-04-19 20:10:02

+0

@ user1344736如果cds是您的表格並且ppl是該表格中的一列,那麼您可以這樣做:從cds中選擇ppl或從cds中選擇*。你無法做的是:select * from cds.ppl – 2012-04-19 20:50:32

-1

經過一番研究,我編輯了我的答案。

關於EXECUTE (string-expression)聲明,是的,您必須爲字符串表達式使用單引號而不是雙引號。 This頁提到:

It lets you execute dynamically prepared statements, such as statements that are constructed using the parameters passed in to a procedure. Literal strings in the statement must be enclosed in single quotes, and the statement must be on a single line.

這將消除列沒有發現錯誤,但過程將返回此其他錯誤:試圖執行僅此語句時

Result set not permitted in '<batch statement>' 

同樣的錯誤返回:

execute ('select * from sysusers') 

With probable cause

You attempted to execute a SELECT statement in a context where a result set is not permitted.

看到我最近的解決方案的答案。

和關於架構,這裏是如何引用對象:

It is always good practice to refer to database objects by a schema name and the object name, separated by a period (.). For a complete example, to SELECT records from the Employee table in the HumanResources schema of the current database would look like:

SELECT * FROM HumanResources.Employee 

To reference an object located in a remote database, the fully qualified object name includes the server name and the database name. For example, to SELECT records from the Employee table in the HumanResources schema in the AdventureWorks database on MyServer would look like:

SELECT * FROM MyServer.AdventureWorks.HumanResources.Employee 

我測試了在SQL Anywhere中12和它的工作原理是相同的。雖然我不熟悉的模式,就是我建議你下面是實際使用的模式,會的dbowner是架構名稱:

1)select * from dbname.dbowner.tablename

2)select * from dbowner.tablename

3)select * from dbname..tablename (假設表中dbo模式存在)

底線....在SELECT語句cds.ppl必須是一個名爲在cds模式中創建ppl表。

或者,如果cds是你的數據庫名稱和dbo模式中創建ppl你的表名,你缺少一個點:

select * from cds..ppl 
+0

顯然你不熟悉[模式](http://msdn.microsoft.com/en-us/library/dd283095(v = SQL.100).aspx )。 – GSerg 2012-04-19 23:43:19

+0

@GSerg我很誠實,我不熟悉模式。但是,迄今爲止,user1344736對cds.ppl甚至在詢問完之後一直沒有任何聲音,甚至沒有提及他正在使用的SQL Anywhere版本,從而使您的投票和假設遭到濫用。當user1344736表示使用SQL Anywhere時,您的鏈接指向Microsoft SQL Server 2005,2008的相關頁面。 – 2012-04-20 03:23:36

+0

@GSerg你認爲它必須處理這些引號?老實說,你也可以自己投票,因爲明顯地使用撇號而不是引號並不能解決問題。再次閱讀user1344736對您的答案的評論。 – 2012-04-20 03:32:32

2

問題曾與語法和RESULT條款做;在添加分號,RESULT子句並使用SET初始化Sql變量之後,以下是工作原理(在SQL Anywhere網絡服務器版本12.0中進行了測試)。1):

drop proc pplAnalysis; 

CREATE PROCEDURE pplAnalysis() 
RESULT (cnot CHAR(5), cnull CHAR(5), vnot VARCHAR(5), vnull VARCHAR(5), explanation VARCHAR(50)) 
BEGIN 

    DECLARE @Sql NVARCHAR(4000); 

    SET @Sql = 'select cnot, cnull, vnot, vnull, explanation from dbo.spaces'; 

    EXECUTE (@Sql); 

END; 

spaces是一個表,在dbo模式和這些列是在RESULT

指定的相同類型測試這兩個執行該過程和兩個返回的結果的方法:

call pplAnalysis(); 

cnot cnull vnot vnull explanation           
----- ----- ----- ----- -------------------------------------------------- 
Execution time: 0.027 seconds 
Procedure completed 

exec pplAnalysis; 

cnot cnull vnot vnull explanation           
----- ----- ----- ----- -------------------------------------------------- 
Execution time: 0.018 seconds 

˚F或更多的細節:

Returning result sets from procedures

Create procedure statement

1

嘗試先保存在一個時間表的查詢結果,然後從時態表做SELECT

SELECT @Sql = "select into #temp * from cds.ppl" 

EXECUTE(@Sql) 

SELECT * FROM #temp 
+0

從cds.ppl中選擇#temp *。給出錯誤 – 2014-01-09 05:16:09

相關問題