我可以在 Sybase 中使用 MS SQL 语法吗? [英] Can I use MS SQL syntax in Sybase?

查看:24
本文介绍了我可以在 Sybase 中使用 MS SQL 语法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾研究过 SQL Server 数据库.现在我必须处理 Sybase 数据库(使用 Squirrel 客户端).此查询不起作用:

I have worked on SQL Server database. Now I have to work on a Sybase database (using a Squirrel client). This query is not working :

DECLARE @tableName VARCHAR(500);
DECLARE my_cursor CURSOR FOR

SELECT name
FROM   sysobjects
WHERE  type = 'U';
OPEN my_cursor;
FETCH NEXT FROM my_cursor INTO @tableName;
WHILE @@FETCH_STATUS = 0
    BEGIN
        //Do something here
        FETCH NEXT FROM my_cursor;
    END
CLOSE my_cursor;
DEALLOCATE CURSOR my_cursor; 

它给出了一个错误 - 关键字FROM"附近的语法不正确.SQLState: ZZZZZ错误代码:156错误发生在:FETCH NEXT FROM my_cursor INTO @table_Name

It gives an error - Incorrect syntax near the keyword 'FROM'. SQLState: ZZZZZ ErrorCode: 156 Error occured in: FETCH NEXT FROM my_cursor INTO @table_Name

现在这在 SQL Server 数据库中工作正常(在我将最后一行更改为 DEALLOCATE my_cursor 之后).谁能告诉我哪里出错了?

Now this works fine in a SQL Server database (after I change the last line to DEALLOCATE my_cursor). Can anybody tell me where I am going wrong?

推荐答案

正如 Mitch 指出的 fetch 语法是:

As Mitch points out the fetch syntax is:

fetch cursor_name [into fetch_target_list]

您还需要在单独的批处理中声明游标,这意味着您必须在声明语句之后放置一个GO".然后您会发现您的变量超出了范围,因此您需要将其移动到GO"之后.

You also need to declare the cursor in a separate batch, this means you must put a "GO" after the declare statement. You will then find that your variable drops out of scope, so you'll need to move that so that it's after the "GO".

您还需要检查@@sqlstatus 以查看获取的成功程度,而不是@@FETCH_STATUS,我认为它只是 MSSQL.

You also need to examine @@sqlstatus to see how successful the fetch was, rather than @@FETCH_STATUS which I think is MSSQL only.

DECLARE my_cursor CURSOR FOR
  SELECT name
  FROM   sysobjects
  WHERE  type = 'U'
go

DECLARE @tableName VARCHAR(500)
set nocount on
OPEN my_cursor
FETCH my_cursor INTO @tableName

WHILE @@sqlstatus = 0
  BEGIN
      --Do something here

      FETCH my_cursor INTO @tableName
      print @tablename
  END

CLOSE my_cursor
DEALLOCATE CURSOR my_cursor

在 Sybase ASE 中,行尾不需要分号.

And no semicolons needed at the end of lines in Sybase ASE.

这篇关于我可以在 Sybase 中使用 MS SQL 语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆