I have a procedure TempSalesUpdateID which gets all ProductName's column in TempSales table and set it's corresponding ProductID.

ALTER PROCEDURE [dbo].[TempSalesUpdateID]
SET        T.ProductID = P.ProductID 
FROM       TempSales T 
INNER JOIN Products P 
ON         P.PN=T.ProductName

When I execute it through SQL Server Management Studio it runs perfectly: when it finds a corresponding ID, it fills ProductID column, otherwise leaves it NULL.

When I run it from my C# Windows Form Application, it puts almost random numbers instead of leaving NULL (the ones which doesn't have a corresponding ID).

I say almost because: 13098 rows have value 1 instead of NULL, 14742 rows have value 2 instead of NULL, 13601 value 3, etc.

Could you please help me? I'm really lost here

  • Nothing in the question points to any possible reason. Are you sure your C# form is calling the same procedure, in the same schema, in the same database? Can you check that your C# app does not use a connection string with AttachDbFileName and validate that it is using the same instance etc. as Management Studio? Are you checking the results in the app or in SSMS? – Aaron Bertrand Mar 10 '17 at 18:32
  • I'm sure it's the same procedure, same schema, same database. It doesn't use a connection string with AttachDbFileName. I'm checking results in SSMS. – G. Knoxx Mar 10 '17 at 23:23
  • Are you maybe running into a collation issue here? Is the join on character columns somehow messing up capitalization and accents in the PN and ProductName value comparison? It would be interesting to see how the values that are being incorrectly set to 1,2,3 etc. look compared to what you expect. – sql_williamd Mar 11 '17 at 20:54
  • I don't know why but if I use a new connection to run the procedure, it works. Can someone explain me why this happens? – G. Knoxx Mar 13 '17 at 14:03

The problem was solved by using a new connection to execute the procedure.

I think it might help if someone is on the same situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.