score:1
Accepted answer
You can use this sample, i used a system table to get the first 10 rows, but you can create your own cursor.
DECLARE @cursor_name AS 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
To the cursor name is added a guid, to guarantee that is always different.
Then a query is created and executed based on that name.
Hope it helps!
score:0
You need to run it inside a dynamic sql statement. As a starting point you can check this question: Using a cursor with dynamic SQL in a stored procedure
More questions
- How to name a cursor dynamically in SQL Server
- How to Update a column by getting the column name dynamically from another table in sql server
- How to use database name dynamically in SQL Server
- How to give table name dynamically in stored procedure in SQL Server 2012
- How to connect an existing SQL Server login to an existing SQL Server database user of same name
- How can I run just the statement my cursor is on in SQL Server Management Studio?
- Sql Server 2005 how to change dbo login name
- How to create duplicate table with new name in SQL Server 2008
- How to turn one column of a table into a csv string in SQL Server without using a cursor
- How can I query the name of the current SQL Server database instance?
- How to get the logical name of the transaction log in SQL Server 2005
- How to change JSON Column name in sql server 2016
- How to get the Index Name from IndexId in SQL Server 2005
- How to find the name of stored procedure, based on table name search, using SQL Server 2008?
- How to combine first name, middle name and last name in SQL server
- How to get the ROOT node name from SQL Server
- How to create daily backup with unique name in sql server
- How to find out user name and machine name to access to SQL server
- A cursor with the name ... already exists - SQL Server 2008
- How to write a cursor inside a stored procedure in SQL Server 2008
- How do we alias a Sql Server instance name used in a Connection String .config?
- Dynamically name indexes in SQL Server 2005?
- SQL Server How Do You Iterate Though a Cursor Loop Once When a Specific Condition Occurs
- How do I use SQL Server table name in select query with a variable?
- How to change Instance name in SQL Server
- Set database name dynamically in SQL Server stored procedure?
- How to dynamically decide what tables to join in SQL Server stored procedure
- How to get table name within a 'select' statement in SQL Server
- How do I remove database name from SQL Server generated script?
- How to handle spaces in database name for Sql server CREATE TABLE?
More questions with similar tag
- How to easily add columns to a temp table?
- t-sql join but keep the row of joined table
- What do the *= and =* operators do in T-SQL?
- Are these tables too big for SQL Server or Oracle
- How display zero value if there is no value matched in linked tables
- How to declare a nullable local variable in t-sql?
- Trying to write a SELECT statement but keep getting can't convert to int error
- How do I open a DTSX file in Visual Studio 2019?
- Using JSON_MODIFY(), how do I append a string to the root path when its name is unknown or is unspecified?
- How to make 1 parameter optional in a SQL stored procedure
- Highly dispair execution times when changing comparision value on query
- Frustrating problem connecting to SQL Server Express from C# code
- add to primary key without dropping foreign key references
- Sql query optimization using IN over INNER JOIN
- SQL Server, is this correct index usage?
- Need help understanding this TSQL join behavior
- How to make sum of rows value until sum value is 1.0 in SQL Server
- update table with random values
- Get New CustomerID using Self Join in SQL Server
- FIND all tables I need to join to get relationship between two tables
- Use a Sequence in a function SQL Server
- sql query to show average of each tag present in proficy historian
- Does -1 always mean "max" in SQL Server metadata table?
- Insert rows based on another row
- SQL transpose full table
- while generating insert script as dynamic query, this N is not getting prefixed even though the column is nvarchar
- how to retrieve password from database and post on text box as *
- Remove milliseconds from date time field in SQL Server 2008
- Stored procedure to update multiple tables
- SQL Server - How to filter rows based on matching rows?
Source:
stackoverflow.com