2012-04-19 69 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