April 11, 2005 at 12:20 pm
I have three tables Orders, Employees and TempOrders. Orders and Employees are linked by EmployeeID. Orders and Employees are in database A and TempOrders is in database B.
I want to extract information from Orders and Employees in order to populate TempOrders. Here is an example of what I'm doing:
When I run the sp I get a message saying that it completed but when I check TempOrders there are no records. If I run the SELECT statement on its own the expected records are returned.
Basically, the idea is to retrieve all orders from the Orders table that haven't already been retrieved. I know that there will probably be easier ways of achieving this but the method that the compant has selected is this.
Hope this makes sense!
April 11, 2005 at 12:31 pm
Try this :
WHERE B.dbo.TempOrders.OrderID IS NULL
April 11, 2005 at 12:48 pm
Hmmmm.... By process of elimination, if the "select" portion of the code works, then there must be some problem with the "insert" portion. Try removing [dbo]. from the column names in the insert statement.
Next, as a test case, try the following insert, substituting your own values for these test values.
If the insert test works, and the select works, then the problem is with the data being returned by the select. For example, you may be attempting to insert a null value into a "not null" column.
If you still have difficulties, please post the DDL for the TempOrders table. This will help with troubleshooting.
Hope this helps
Wayne
April 11, 2005 at 1:24 pm
Hi Wayne,
Oddly enough, I restricted the SELECT statement to one row and attempted to manually add the data that was returned. I found that manually entering the data into the table or by passing the values (as you suggested) to an INSERT statement works perfectly?!
Unfortunately, I don't have access to the database at the moment so I can't provide the table info you asked for. I'll post it as soon as I have access.
Thanks for your help,
David
April 11, 2005 at 2:18 pm
Ok. Next test to see what happens:
Replace the sample values with valid ones and run the insert. Assuming that this works, and it probably will, then the issue is almost certainly with the data returned from the full select.
Good luck
Wayne
April 12, 2005 at 3:40 am
I reversed the procedure so that I returned one row of data and read the values into variables that I declared using the exact datatypes and lengths of the columns within the TempOrders table.
I then used the INSERT statement and passed the values that were stored within the variables. When I ran the procedure it updated the table correctly?!
E.g.
Very odd!
April 12, 2005 at 3:52 am
Probably the first row is correct and one of the other rows is causing problems - either because of duplicity, or because of data type or some constraint violation.
Extract the entire procedure (i.e. the real code in it, not the procedure definition) into QA window, if there are any parameters, declare them, set their values and run it as SQL script. In QA you should receive error message that could show you where to look ... although often it is just a hint and you still have some work to find out where the problem really is.
BTW, did you change the = NULL to IS NULL?
/edit/
One more thing...
April 12, 2005 at 4:18 am
I am really confused now! When I copied the code to QA and executed it the table was populated? So for whatever reason, the code does not work when executed as part of the stored procedure but does work if executed from QA? Any ideas on why this may be?
Thanks for your help,
David
April 12, 2005 at 4:50 am
David,
you still didn't answer whether you changed the = NULL to IS NULL. If not, and your procedure is using different settings of ANSI NULLS than QA, then this is the problem.
From BOL:
When SET ANSI_NULLS is ON, a SELECT statement using WHERE column_name = NULL returns zero rows even if there are null values in column_name.
When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To (<> comparison operators do not follow the SQL-92 standard. A SELECT statement using WHERE column_name = NULL returns the rows with null values in column_name.
April 12, 2005 at 4:53 am
Apologies, the code is 'IS NULL' not '= NULL'.
David
April 12, 2005 at 5:05 am
Hmm... there are still other settings that could influence it, like QUOTED IDENTIFIER, DATEFORMAT etc., but I'm afraid I have no ideas until I know DDL for both tables, and some sample data. If there are some implicit conversions involved, this could be the issue - otherwise, I'm at a loss. At least for now :-).
April 12, 2005 at 5:28 am
Having successfully run the code in QA, I tried to run the unamended stored procedure and it too works now! Could it have been some sort of security issue on the table or stored procedure?
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply