473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Deleting multiple records from a datagrid using cursors

Hi Friends...
Can anyone plz help me out i'm stuck ....
I want to delete multiple records from a data grid using cursors.the parameter for the stored procedure is an comma seperated string(of User_ID),but the stored procedure which i had written is not working can anyone help me out,it would be a great help for me.when i execute the procedure and print @@FETCH_STATUS the status it prints -1 ie no record ....

CREATE PROCEDURE test_cur3(@User_ID varchar(100))
as

SET NOCOUNT ON

declare @UID char(15)
DECLARE TESTCUR3 CURSOR for
SELECT User_ID FROM Header WHERE User_ID in ( @User_ID)

open TESTCUR3

FETCH NEXT FROM TESTCUR3
INTO @UID
WHILE @@FETCH_STATUS = 0
BEGIN


update Header set Deleted='1' WHERE User_ID= @UID


FETCH NEXT FROM TESTCUR3
INTO @UID
END;

CLOSE TESTCUR3
DEALLOCATE TESTCUR3
GO
Sep 13 '07 #1
11 2765
davef
98
How many User IDs are you trying to send? My first guess is that your sp could possibly work with a single ID. I understand that you concatenate multiple User IDs in a comma delimited string along the lines of '1,2,3,4'. That becomes your input parameter, which the query is unable to match:

SELECT User_ID FROM Header WHERE User_ID IN ('1,2,3,4')

Massage the parameter to modify your query (if you haven't done so yet):

SELECT User_ID FROM Header WHERE User_ID IN ('1','2','3','4')
Sep 13 '07 #2
Hi...

Thnks for your reply,but i had already tried to Massage the parameter to modify the query
SELECT User_ID FROM Header WHERE User_ID IN ('1','2','3','4')

but still it's not working, can u plz suggest me some other way to solve this problem.
Sep 14 '07 #3
amitpatel66
2,367 Expert 2GB
Hi...

Thnks for your reply,but i had already tried to Massage the parameter to modify the query
SELECT User_ID FROM Header WHERE User_ID IN ('1','2','3','4')

but still it's not working, can u plz suggest me some other way to solve this problem.
Have you tried by passing a single value?

Check if that is working and POST back. If the procedure is working with single value then we could trace out if the problem is with multiple value being passed or with the procedure itself.
Sep 14 '07 #4
amitpatel66
2,367 Expert 2GB
Hi Friends...
Can anyone plz help me out i'm stuck ....
I want to delete multiple records from a data grid using cursors.the parameter for the stored procedure is an comma seperated string(of User_ID),but the stored procedure which i had written is not working can anyone help me out,it would be a great help for me.when i execute the procedure and print @@FETCH_STATUS the status it prints -1 ie no record ....

CREATE PROCEDURE test_cur3(@User_ID varchar(100))
as

SET NOCOUNT ON

declare @UID char(15)
DECLARE TESTCUR3 CURSOR for
SELECT User_ID FROM Header WHERE User_ID in ( @User_ID)

open TESTCUR3

FETCH NEXT FROM TESTCUR3
INTO @UID
WHILE @@FETCH_STATUS = 0
BEGIN


update Header set Deleted='1' WHERE User_ID= @UID


FETCH NEXT FROM TESTCUR3
INTO @UID
END;

CLOSE TESTCUR3
DEALLOCATE TESTCUR3
GO
Is yur code displaying any error??
I think you missed COMMIT in the procedure if it is not throwing any error?
Sep 14 '07 #5
Hi..

As u suggested I tried by passing a single value ,but still it's not working and even my code is not displaying any error ...I'm not able to undersatand where exactly i had made the mistake...
Sep 14 '07 #6
davef
98
Hi..

As u suggested I tried by passing a single value ,but still it's not working and even my code is not displaying any error ...I'm not able to undersatand where exactly i had made the mistake...
Put a Print statement after
Expand|Select|Wrap|Line Numbers
  1. SELECT User_ID FROM Header WHERE User_ID in ( @User_ID)
and run this parsed query in the analyzer or Mgmt Studio. See if it fetches any results.
Sep 14 '07 #7
amitpatel66
2,367 Expert 2GB
Hi Friends...
Can anyone plz help me out i'm stuck ....
I want to delete multiple records from a data grid using cursors.the parameter for the stored procedure is an comma seperated string(of User_ID),but the stored procedure which i had written is not working can anyone help me out,it would be a great help for me.when i execute the procedure and print @@FETCH_STATUS the status it prints -1 ie no record ....

CREATE PROCEDURE test_cur3(@User_ID varchar(100))
as

SET NOCOUNT ON

declare @UID char(15)
DECLARE TESTCUR3 CURSOR for
SELECT User_ID FROM Header WHERE User_ID in ( @User_ID)

open TESTCUR3

FETCH NEXT FROM TESTCUR3
INTO @UID
WHILE @@FETCH_STATUS = 0
BEGIN


update Header set Deleted='1' WHERE User_ID= @UID


FETCH NEXT FROM TESTCUR3
INTO @UID
END;

CLOSE TESTCUR3
DEALLOCATE TESTCUR3
GO
First of all Check yur data if it exist that you are trying to UPDATE.

Probably you can try executing a function with some print statements to check what is the value passed as input parameter and whether the CURSOR is getting executed properly!!
Sep 14 '07 #8
if i pass the parameter in the following format it does return result but still the update fails
SELECT User_ID FROM Header WHERE User_ID in ( '1000010001','1000010002')
Sep 14 '07 #9
davef
98
if i pass the parameter in the following format it does return result but still the update fails
SELECT User_ID FROM Header WHERE User_ID in ( '1000010001','1000010002')
The chances are that you mess up with multiple User IDs in the IN clause. Your sp works perfectly fine with a single parameter. Here's a solution:

1. Create an auxiliary split function like this:

Expand|Select|Wrap|Line Numbers
  1. CREATE FUNCTION dbo.Split
  2. (
  3.     @RowData nvarchar(2000),
  4.     @SplitOn nvarchar(5)
  5. )  
  6. RETURNS @RtnValue table 
  7. (
  8.     Id int identity(1,1),
  9.     Data nvarchar(100)
  10. AS  
  11. BEGIN 
  12.     Declare @Cnt int
  13.     Set @Cnt = 1
  14.  
  15.     While (Charindex(@SplitOn,@RowData)>0)
  16.     Begin
  17.         Insert Into @RtnValue (data)
  18.         Select 
  19.             Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))
  20.  
  21.         Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
  22.         Set @Cnt = @Cnt + 1
  23.     End
  24.  
  25.     Insert Into @RtnValue (data)
  26.     Select Data = ltrim(rtrim(@RowData))
  27.  
  28.     Return
  29. END
  30.  
  31.  
2. Modify your sp:

Expand|Select|Wrap|Line Numbers
  1. SET ANSI_NULLS ON
  2. GO
  3. SET QUOTED_IDENTIFIER ON
  4. GO
  5. ALTER PROCEDURE [dbo].[test_cur3](@User_ID varchar(100)) 
  6. as
  7.  
  8. SET NOCOUNT ON
  9.  
  10. declare @UID char(15)
  11. DECLARE TESTCUR3 CURSOR for
  12. SELECT [User_ID] FROM Header WHERE [User_ID] in ( select Data from dbo.Split(@User_ID, ','))
  13.  
  14. open TESTCUR3
  15.  
  16. FETCH NEXT FROM TESTCUR3
  17. INTO @UID 
  18. WHILE @@FETCH_STATUS = 0
  19. BEGIN
  20.  
  21.  
  22. update Header set Deleted='1' WHERE [User_ID]= @UID 
  23.  
  24.  
  25. FETCH NEXT FROM TESTCUR3
  26. INTO @UID
  27. END;
  28.  
  29. CLOSE TESTCUR3
  30. DEALLOCATE TESTCUR3
  31.  
3. Call your sp:

Expand|Select|Wrap|Line Numbers
  1. exec test_cur3 '1000010001,1000010002,1000010003'
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648
Sep 14 '07 #10
Hi Friends...
Can anyone plz help me out i'm stuck ....
I want to delete multiple records from a data grid using cursors.the parameter for the stored procedure is an comma seperated string(of User_ID),but the stored procedure which i had written is not working can anyone help me out,it would be a great help for me.when i execute the procedure and print @@FETCH_STATUS the status it prints -1 ie no record ....

CREATE PROCEDURE test_cur3(@User_ID varchar(100))
as

SET NOCOUNT ON

declare @UID char(15)
DECLARE TESTCUR3 CURSOR for
SELECT User_ID FROM Header WHERE User_ID in ( @User_ID)

open TESTCUR3

FETCH NEXT FROM TESTCUR3
INTO @UID
WHILE @@FETCH_STATUS = 0
BEGIN


update Header set Deleted='1' WHERE User_ID= @UID


FETCH NEXT FROM TESTCUR3
INTO @UID
END;

CLOSE TESTCUR3
DEALLOCATE TESTCUR3
GO

If you pass argument like '1,2,3,4' , the database will assume input as single element and it won't assume that the input is a collection of elements seperated by comma,

Please pass input like the above and user defined split method to split the elements.
Sep 17 '07 #11
thnks a lot for your reply ...i had tried with split funtion and it's working ,but was trying without using split funtion ,thought there might be some other way out...

anyway's thnks a lot guys for ur help .....
Sep 20 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Alban Hertroys | last post by:
Oh no! It's me and transactions again :) I'm not really sure whether this is a limitation of psycopg or postgresql. When I use multiple cursors in a transaction, the records inserted at the...
0
by: Ioannis Demetriades | last post by:
Hi, I am doing a VB.NET application with SQL Server 2000 and I need to implement a database search facility that will normally return lots of records. Those records will then be displayed in a...
13
by: Bob Darlington | last post by:
I have a repair and backup database routine which runs when a user closes down my application. It works fine in my development machine, but breaks on a client's at the following line: If...
5
by: Mojtaba Faridzad | last post by:
Hi, with SetDataBinding( ) a DataGrid shows a DataView. user can select some rows in the grid by holding cotrol key. when user clicks on Delete button, I should delete all selected rows. I am...
1
by: Wolffang | last post by:
From: "Wolffang" <javid@techlite.co.uk> Subject: How to do a multiple select and update in a datagrid Date: 23 October 2004 21:28 Using Visual studio.net VB I have a datagrid that brings...
0
by: Patrick.O.Ige | last post by:
I have a datagrid with checkboxes.. When a user clicks one check box and clicks the delete button it deletes that ROw. There another situation when a user clicks multiple rows so i had to loop...
20
by: p175 | last post by:
Hi people, I have a stored procedure that creates many Global temporary session tables. Into each of these tables go the results of various processing using relational division all keyed and...
1
by: Wan | last post by:
Hi, I have a test project consists of two forms. The main form contains a datagrid and a button. On click of button I populate the datagrid with a ds.table(0) so far so good and on grid's...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered by entering the Date, Shift (morn, eve, or night)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.