views:

566

answers:

6

Hi everyone, this is my first time using this site.

OK, i need to use a cursor to call a stored procedure that has 2 parameters that i need to pass into from Customers table.

Here is what i mean;

My goal is to pass all the CustomerID and CustomerName from Customers table into my stored procedure called AddCustomers which has 2 parameters of CustomerID and CustomerName. ie: AddCustomer [CustomerID],[CustomerName] [side note: this AddCustomer stored procedure does some filtering things that i need for my apps]

So the end result of using this CURSOR with an stored procedure is to DUMP/PASS all of customerID and CustomerName from Customers table into AddCustomer stored procedure.

I have used sp_executesql, but not successfully.

Here is what i have tried but not working.

Declare @CustomerID int
Declare @CustomerName varchar(100)

Declare cur CURSOR READ_ONLY
FOR
SELECT CustomerID, CustomerName
from Customers

OPEN cur

FETCH NEXT FROM cur
INTO @CustomerID, @CustomerName

WHILE @@FETCH_STATUS = 0
BEGIN
   EXEC AddCustomer @CustomerID, @CustomerName 
   FETCH NEXT FROM cur
   INTO @CustomerID, @CustomerName
END

CLOSE cur
DEALLOCATE cur


I am new to the use of cursors so I am not sure why this isn't working. If you can provide any clues or links, I'd would appreciate it.

A: 

Why are you using a cursor? Do you realize that's the least efficient, most complex way to approach this problem? There's a much better than even chance that you'd be better off with a modified version of your stored procedure. Or one of several other patterns.


Again, the simplest approach would probably be to modify the stored procedure (making a new one, if you want) for input.

Or, you could use your language of choice C#, VB, whatever) to query for the customers, and loop there on the stored procedure.

How do you intend to handle exceptions otherwise/

le dorfier
it is required. So i have no choice.
Sam
It's actually not in this case. You cannot call a sproc in set based manner like a UDF.
James
Why? I can't imagine a reasonable suggestion unless I can imagine a problem that's reasonable to solve.
le dorfier
are u sure? So it is NOT possible to loop thru a Stored Procedure with a cursor?
Sam
Le Dorfier,What is going on is that i need to re-populate a table called CustomerFilter from Customers table with all of the CsutomerID and CustomerName from Customers. I just want to write a script to automatically dump the data from Customers to CustomerFilters with my existing AddCustomers SP.
Sam
Ok, I think Mark is the better cursors guy here. It can be done, but SQL Server doesn't give you much help, as you're finding out. I suspect you can imagine how to do what you want in a .NET language? To me that would be easier with better debugging resources. If you really want to figure out ...
le dorfier
... cursors, then inside the loop use intermediate variables so you are sure what data types are being used everywhere. Then it's a handoff to Mark.
le dorfier
my goal has NOTHING to do with .NET or my app. it is simply that batch script that i need to create for QA to run so that the production server will have the all filtered data onto the CustomerFilter table.
Sam
U guys are way too ahead and think too deep into this.I just want a script to dump data from table A to B
Sam
Thanks for the vote of confidence le dorfier. I had to laugh reading this exchange...
Mark Brittingham
+1  A: 

Your cursor is fine from syntax perspective. I'm guessing that the issue has something to do with your stored procedure throwing an error. You should post the code for the stored procedure as well.

And the error message(s).

James
I think the SQL i wrote above is not right.To answer ur question, the AddCustomer stored procedure simply add a row to a table called CustomerFilter.ie: AddCustomer 1, 'John Doe'Calling this above would just add a row in CustomerFilter table.Nothing fancy.
Sam
Then just "SELECT CustomerID, CustomerName INTO CustomerFilter FROM Customers".Or "INSERT INTO CustomerFilter (CustomerId, CustomerName) SELECT CustomerId, CustomerName FROM Customers"
le dorfier
It is NOT that simple.As i said before, the AddCustomer Stored Proc is doing some filtering on CustomerName. So when finish executing this SP, the Name will be different from the original CustomerName in Customers table.Am i getting my message across?
Sam
A: 

OK, if Cursor CANNOT loop thru stored procedure, THEN can anyone provide a solution or hints to my problem, which is to PASS all CustomerID and CustomerName from Customers table into my AddCustomer stored procedure so that i can do some filtering stuff on each of the CustomerID and CustomerName that i pass into AddCustomer Stored procedure.

thanks in advance.

Sam
what i need is a script.
Sam
Sam, could you post the error that you are getting from this script? What you are doing is entirely reasonable.
JoshL
i dont have access to my database now since i am on the network anymore.But the errors was something about the Table.
Sam
Just to let you know - as in my answer below - calling a stored procedure from within a cursor is a normal use of cursors - it is *not* illegal.
Mark Brittingham
A: 

Sam,

There are times when using a stored procedure to do a task like this is perfectly appropriate. Le Dorfier has certainly given me lots of good advice but I have to disagree with him on this one. Especially if this is a one-time processing of your records to achieve some form of transformation, then you are really close to your solution as I don't see any obvious flaws in your code. BTW, I've taken the same approach on more than one occasion and it has worked very well.

So...post the stored procedure as well as we'll see if that doesn't help. Also, what specific error are you seeing?

UPDATE: Wait, are you just running this in the SQL Window? I usually package this stuff up in another stored procedure (often just a temporary one) and then run it. Of course, it may also be that you just need to execute this code - not using EXEC but just using the Execute button...it is hard to tell based on your description.

UPDATE 2: You say above that the error is something about the table. Are you sure that the data types that you have defined are correct? Could the error be a mismatch between the data pulled from the table and the type of the var into which you are placing it?

Mark Brittingham
I just need to create a script that will automatically pass all of the CustoemrID and CustomerName into another table using that Stored procedure called AddCustomer. It DOESNT matter how i execute it.
Sam
The data type should be the same. I have taken the time to examine it.They are uniquIndentifier for CustoemrID and varchar for customerName
Sam
I think a uniqueidentifier is not an integer - it's a guid.
le dorfier
yes, it is guid. SO?
Sam
Sam, as I show on my answer, you are using the wrong type on the customerid variable.
eglasius
Your code says "Declare @CustomerID int".
le dorfier
+1  A: 

There doesn't seems to be anything wrong with the syntax. We really need the error messages in order to provide an answer.

Being blind my best shot would be the varchar(100) ... perhaps the table has it nvarchar ...

There doesn't seems to be anything wrong with the syntax. We really need the error messages in order to provide an answer.

Being blind my best shot would be the varchar(100) ... perhaps the table has it nvarchar ...

Update 1: k, you mentioned on a comment

"The data type should be the same. I have taken the time to examine it. They are uniquIndentifier for CustoemrID and varchar for customerName"

Notice you are using int on the customerid variable. Change it to: Declare @CustomerID uniqueidentifier

eglasius
i have changed to uniqueIdentifier, it is the same. It was a typo when i entered this.
Sam
Sounds like there's not much more we can do until you can check the details online. Maybe start with a script that assigns test values to the variables and executes the SP with the same syntax and parameters, but not in the cursor loop.
le dorfier
No sweat, we all go through this - it's always easy the second time. :D
le dorfier
A: 

Ok, Guys, Thanks for all ur efforts and replies.

Guess what? It works now. I dont know why? I think there was something wrong with my database permission setting. It is working now after i checked a permission from the table.

Thx. This site is great!

Sam
Um, Sam, it is customary and appropriate to upvote all of the helpful answers to thank people for attempting to help you with your problem. I'm going to upvote Freddy and le dorfier now because I think they helped.
Mark Brittingham