From: http://lwlzyjl.cnblogs.com/
Method 1:
Create Table # tabtmp (id int)
Insert # tabtmp execute ('select ID from '+ @ tablename)
Declare curone cursor
Select ID from # tabtmp
Open curone
Fetch next from curone into @ ID
Method 2:Exec ('
Declare curone cursor global
For '+ @ asql );
Create procedure [dbo]. [pro_CURSOR]
AS
BEGIN
-- Declare a cursor
DECLARE MyCURSOR CURSOR
SELECT userid, depid FROM users
-- Open the cursor
Open MyCURSOR
-- Declare two variables
I don't know who was the first to make such a mistake, and everyone always thought Rs.Open sql,1,1 opened a forward-only read-only cursor.
But yesterday accidentally found that Rs.Open sql,conn,0,1 is open only forward read-only cursor, I was changed after the discovery speed is a lot of:), but I use Rs.Open
Label:Declare a
Declare b
Declare c
Declare ...
Declare cursor_name Cursor--declaring
a cursor for (SELECT * from table) querying an arranged table
open cursor_name opening a cursor
[SQL]-- 1. Update the salary of each teacher to the original salary + bonus-- Defines two variables to store ttid and rewardDeclare @ tid intDeclare @ reward money-- 1. Create a cursor Based on the bonus tableDeclare cur_reward cursor fast_forward for select ttid, reward from TblTeacherSalary-- 2. Open the cursorOpen cur_reward-- Read data through a cursorFetch n
The following article mainly describes the SQL Server cursor instance, if you encounter a SQL Server cursor instance in the actual operation, but you do not know how to apply correctly to it, then the following article to you must be a mentor, hope will give you some help in this respect.
Cursor
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> Declare @ ID Int , @ PID Int , @ Ver Int Declare # Cur Cursor For Select 1 As ID, 1 As Parentid, 1 As Version Union Select 2 , 1 , 2 Union Select 3 ,
Cursor type
Work on table
Rows retrieve
Scrolling type
SQL statement
Fast?
Read Only?
Client?
Server?
Open cost
Fetche cost
Scroll overhaed
Impact tempdb
Concurrency
Lock
Forward-only
Directly on table
Until fetch
Only fetch next
Fast_forward
Fast
Yes
Yes
Yes
Low (not tempdb)
Lo
Code
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Declare @ field1 int, @ field2 varchar (40), @ field3 varchar (40)
Declare mycur cursor for select field1, field2, field3 from Table1Open mycur/* Open cursor */
Fetch next from mycur into @ field1, @ field2, @ field
Table data:
ID name
1
2 B
3 C
Stored Procedure:
Set ansi_nulls on
Set quoted_identifier on
Go
Alter procedure [DBO]. [test1]-- @ ID varchar (50) OutputAs
Declare @ ID varchar (50) -- set the intermediate variableDeclare vari_cursor cursor scroll for select ID from IBS -- declares the cursor and points to the query result. The local parameter indicat
Oracle stored procedure: cursor and dynamic SQL 1. create package + CURSOR SQL code CREATE OR REPLACE PACKAGE RefCursor IS type t_RefCursor IS ref CURSOR; END RefCursor; 2. create a stored procedure and define the in and out parameters.
In general, there is not much chance of using bulk fetch, but Oracle provides this functionality we'd better get familiar with it and maybe when it will be used.
Copy Code code as follows:
Declare
Cursor C1 is select * from T_depart;
V_depart T_depart%rowtype;
Type V_code_type is table of T_depart.depart_code%type;
V_code V_code_type;
Type V_name_type is table of T_depart.depart_
-- Delete viewDrop viewFour: CursorsThe essence of relational database management system is set-oriented, in MS SQL SERVER there is no form of expression that describes a single record in a table. A cursor is actually a mechanism that extracts one record at a time from a result set that includes multiple data records, with cursors typically accompanied by while , cursors use less, because it reduces the ef
Background:Cursors control the behavior of server-side cursors, with the associated T-SQL as follows:Declare, open, Fetch, close, deallocate.1.Cursor_close_on_commit{on | off};If set to on closes the open cursor when the transaction commits or rolls back,If set to OFF, the cursor will continue to open after the transaction is committed, unless the
Declare @ chrname varchar (30)Declare @ chryhm varchar (30)
Declare dw_only1 cursor for -- set the cursor to retrieve the record set for operationSelect chrname, chryhm from gzry where chrzw> = 5 order by chrzw DESC, intpxh -- select the position from the front and aboveOpe
Alter procedure mktg_differencedailyreport
@ Customername varchar (100 ),
@ Shipdate datetime
As
Begin
Set nocount on
Declare
@ Noreceivedpackages varchar (2000 ),
@ Currbilltrackid varchar (30 ),
@ Currpackageid varchar (30)
Select a. billtrackid, A. Pieces, (Case isnull (A. canceled, 0) when 1 then '√ 'end) as canceled,
C. facilityname as customername, A. shipdate,
Cast (''as varchar (2000) as noreceivedpackageidlist,
Count (B. packagetrackid) as
Define PROCEDURE nbsp; P1 nbsp; (P_Asset_TypeINvarchar2, P_Asset_SubTypeINvarchar2, P_OrderIdsINvarchar2 ). You need to dynamically generate cursor or dynamically Execute SQL DML operations based on input parameters. 1. Simple query SELECT nbsp; ABCfromTabl defines PROCEDURE P1 (P_Asset_Type IN varchar2, P_Asset_SubType IN varchar2, P_OrderIds IN varchar2 ).
You need to dynamically generate
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.