محمد سلیم آبادی
یک شنبه 14 تیر 1388, 04:46 صبح
با سلام.
در اینجا یک اسکریپ در مورد کاربرد کرسر ها آورده ام امید وارم این اسکریپت مفید واقع شود.
از تمام دوستان می خواهم که مطالب مفید در مورد استفاده و کاربرد کرسر ها را در این تاپیک قرار بدهند.
USE tempdb
GO
IF object_Id(N'#temptb')IS NOT NULL
DROP TABLE #temptb
------- Create temporary table
CREATE TABLE #temptb
(
ID uniqueidentifier primary key,
FirstName nvarchar(50),
Gender bit
);
------- Declare variables ----------
DECLARE @firstName nvarchar(50),@Gender bit;
-------- Inserting some rows to the temp table
INSERT INTO #temptb VALUES(NEWID(),'Mohammad', 1)
INSERT INTO #temptb VALUES(NEWID(),'Alireza', 1)
INSERT INTO #temptb VALUES(NEWID(),'Zahra', 0)
INSERT INTO #temptb VALUES(NEWID(),'Maryam', 0)
------- Declare
DECLARE X CURSOR
FAST_FORWARD
FOR SELECT FirstName, Gender FROM #temptb t ORDERBY t.firstname
-- create the cursor set
OPEN X
-- Perform the first fetch and store the values in variables.
FETCH NEXT FROM X INTO @firstName, @Gender
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE@@FETCH_STATUS= 0
BEGIN
if(@Gender=1)
print'Mr.'+@firstName
elseif(@gender=0)
print'Miss.'+@firstName
else print @firstName
FETCH next FROM x
INTO @firstname, @Gender
END
------------ Release the cursor set
CLOSE X
------------ Release the cursor
DEALLOCATE X
Result message
Mr.Alireza
Miss.Maryam
Mr.Mohammad
Miss.Zahra
در اینجا یک اسکریپ در مورد کاربرد کرسر ها آورده ام امید وارم این اسکریپت مفید واقع شود.
از تمام دوستان می خواهم که مطالب مفید در مورد استفاده و کاربرد کرسر ها را در این تاپیک قرار بدهند.
USE tempdb
GO
IF object_Id(N'#temptb')IS NOT NULL
DROP TABLE #temptb
------- Create temporary table
CREATE TABLE #temptb
(
ID uniqueidentifier primary key,
FirstName nvarchar(50),
Gender bit
);
------- Declare variables ----------
DECLARE @firstName nvarchar(50),@Gender bit;
-------- Inserting some rows to the temp table
INSERT INTO #temptb VALUES(NEWID(),'Mohammad', 1)
INSERT INTO #temptb VALUES(NEWID(),'Alireza', 1)
INSERT INTO #temptb VALUES(NEWID(),'Zahra', 0)
INSERT INTO #temptb VALUES(NEWID(),'Maryam', 0)
------- Declare
DECLARE X CURSOR
FAST_FORWARD
FOR SELECT FirstName, Gender FROM #temptb t ORDERBY t.firstname
-- create the cursor set
OPEN X
-- Perform the first fetch and store the values in variables.
FETCH NEXT FROM X INTO @firstName, @Gender
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE@@FETCH_STATUS= 0
BEGIN
if(@Gender=1)
print'Mr.'+@firstName
elseif(@gender=0)
print'Miss.'+@firstName
else print @firstName
FETCH next FROM x
INTO @firstname, @Gender
END
------------ Release the cursor set
CLOSE X
------------ Release the cursor
DEALLOCATE X
Result message
Mr.Alireza
Miss.Maryam
Mr.Mohammad
Miss.Zahra