如何在 SQL 服务器中动态命名游标
How to name a cursor dynamically in SQL Server
谁能告诉我如何在 SQL 服务器中动态声明游标名称(固定名称 + 唯一名称)?
这是为了防止错误
02-25-2018 10:12:01 ERROR (AdHocReportserviceImpl.java:882) : org.springframework.jdbc.UncategorizedSQLException: CallableStatementCallback; uncategorized SQLException for SQL [{call usp_AdHocReport(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}];
SQL state [34000]; error code [16916]; [FMWGEN][SQLServer JDBC Driver][SQLServer]A cursor with the name 'FetchRegion' does not exist.; nested exception is java.sql.SQLException: [FMWGEN][SQLServer JDBC Driver][SQLServer]A cursor with the name 'FetchRegion' does not exist
从多线程 java 应用程序访问它时。我试过在 CURSOR
声明后添加 LOCAL
但它不起作用。
DECLARE FetchRegion CURSOR READ_ONLY FOR
SELECT ......
OPEN FetchRegion
FETCH NEXT FROM FetchRegion INTO @RGN
WHILE @@ROWCOUNT <> 0
BEGIN
.....
FETCH NEXT FROM FetchRegion INTO @RGN
END
CLOSE FetchRegion
DEALLOCATE FetchRegion
您需要在动态 sql 语句中 运行 它。作为起点,您可以检查这个问题:Using a cursor with dynamic SQL in a stored procedure
您可以使用此示例,我使用系统 table 获取前 10 行,但您可以创建自己的游标。
声明@cursor_name 为 NVARCHAR(100)
SET @cursor_name = 'sampleCursor'
+ Replace(Cast(Newid() AS VARCHAR(36)), '-', '')
DECLARE @cursor_sql AS NVARCHAR(max)
SET @cursor_sql = N' DECLARE @name nvarchar(10) DECLARE ' + @cursor_name + N' CURSOR FOR select top 10 name from sys.all_columns OPEN ' + @cursor_name
+ N' FETCH NEXT FROM ' + @cursor_name
+ N' INTO @name WHILE @@FETCH_STATUS <> -1 BEGIN print @name FETCH NEXT FROM ' + @cursor_name + N' INTO @name end CLOSE '
+ @cursor_name + N' DEALLOCATE ' + @cursor_name
PRINT @cursor_sql
EXECUTE Sp_executesql
@cursor_sql
为游标名称添加一个 guid,以保证始终不同。
然后根据该名称创建并执行查询。
希望对您有所帮助!
谁能告诉我如何在 SQL 服务器中动态声明游标名称(固定名称 + 唯一名称)?
这是为了防止错误
02-25-2018 10:12:01 ERROR (AdHocReportserviceImpl.java:882) : org.springframework.jdbc.UncategorizedSQLException: CallableStatementCallback; uncategorized SQLException for SQL [{call usp_AdHocReport(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}];
SQL state [34000]; error code [16916]; [FMWGEN][SQLServer JDBC Driver][SQLServer]A cursor with the name 'FetchRegion' does not exist.; nested exception is java.sql.SQLException: [FMWGEN][SQLServer JDBC Driver][SQLServer]A cursor with the name 'FetchRegion' does not exist
从多线程 java 应用程序访问它时。我试过在 CURSOR
声明后添加 LOCAL
但它不起作用。
DECLARE FetchRegion CURSOR READ_ONLY FOR
SELECT ......
OPEN FetchRegion
FETCH NEXT FROM FetchRegion INTO @RGN
WHILE @@ROWCOUNT <> 0
BEGIN
.....
FETCH NEXT FROM FetchRegion INTO @RGN
END
CLOSE FetchRegion
DEALLOCATE FetchRegion
您需要在动态 sql 语句中 运行 它。作为起点,您可以检查这个问题:Using a cursor with dynamic SQL in a stored procedure
您可以使用此示例,我使用系统 table 获取前 10 行,但您可以创建自己的游标。
声明@cursor_name 为 NVARCHAR(100)
SET @cursor_name = 'sampleCursor'
+ Replace(Cast(Newid() AS VARCHAR(36)), '-', '')
DECLARE @cursor_sql AS NVARCHAR(max)
SET @cursor_sql = N' DECLARE @name nvarchar(10) DECLARE ' + @cursor_name + N' CURSOR FOR select top 10 name from sys.all_columns OPEN ' + @cursor_name
+ N' FETCH NEXT FROM ' + @cursor_name
+ N' INTO @name WHILE @@FETCH_STATUS <> -1 BEGIN print @name FETCH NEXT FROM ' + @cursor_name + N' INTO @name end CLOSE '
+ @cursor_name + N' DEALLOCATE ' + @cursor_name
PRINT @cursor_sql
EXECUTE Sp_executesql
@cursor_sql
为游标名称添加一个 guid,以保证始终不同。
然后根据该名称创建并执行查询。
希望对您有所帮助!