Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
How to run the next line in sql when the previous line is in failure in a .sql file while Executing the .sql file in runtime using C#

I have written sql script like this

SQL
ALTER TABLE [dbo].[STOCKDETAILS] ADD  Constraint DF_STOCKDETAILS_QTY_IN DEFAULT ((0)) FOR [QTY_IN]
GO

ALTER TABLE [dbo].[STOCKDETAILS] ADD Constraint DF_STOCKDETAILS_QTY_OUT DEFAULT ((0)) FOR [QTY_OUT]
GO

ALTER TABLE [dbo].[STOCKDETAILS] ADD Constraint DF_STOCKDETAILS_UNIT_COST DEFAULT ((0)) FOR [UNIT_COST]
GO

ALTER TABLE [dbo].[STOCKDETAILS] ADD Constraint DF_STOCKDETAILS_AMT_IN DEFAULT ((0)) FOR [AMT_IN]
GO

ALTER TABLE [dbo].[STOCKDETAILS] ADD Constraint DF_STOCKDETAILS_AMT_OUT DEFAULT ((0)) FOR [AMT_OUT]
GO

ALTER TABLE [dbo].[STOCKDETAILS] ADD Constraint DF_STOCKDETAILS_ID DEFAULT ((0)) FOR [ID]
GO

ALTER TABLE [dbo].[STOCKDETAILS] ADD Constraint DF_STOCKDETAILS_BATCH_NO DEFAULT ((0)) FOR [BATCH_NO]
GO

ALTER TABLE [dbo].[PRODUCT] ADD Constraint DF_PRODUCT_SERIALIZED DEFAULT ((0)) FOR [SERIALIZED]
GO



--------------------------RUN THIS SCRIPT IF GRADEDDETAILS TABLE CONTAINS DATA / IF ERROR OCCURS WHILE CONVERTING THE ABOUVE SRIPT---------
ALTER TABLE GradedDETAILS ADD STOCK_DATE1 DATETIME NULL ----STEP 1--

ALTER TABLE GradedDETAILS ALTER COLUMN [STOCK_DATE] NVARCHAR(100) ----STEP 2--

UPDATE GradedDETAILS SET [STOCK_DATE]=CONVERT(DATETIME, [STOCK_DATE], 104) ----STEP 3--

UPDATE GradedDETAILS SET [STOCK_DATE1]=[STOCK_DATE] ----STEP 4--

ALTER TABLE GradedDETAILS DROP COLUMN [STOCK_DATE] ----STEP 5--

SP_RENAME 'GradedDETAILS.STOCK_DATE1','STOCK_DATE','COLUMN' ----STEP 6--

I want to execute query step by step in sql using C# with single click

if you execute it once then i got error like this

Incorrect syntax near 'SP_RENAME'.

when one line is failed total execution fails due to that error

I have more scripts to run in a single click. would u please resolve this error
Posted
Updated 30-Jul-15 0:41am
v3
Comments
Suvendu Shekhar Giri 30-Jul-15 1:50am    
I am sure, your problem is somehing else because you have placed GO after each statement and it should automatically move to the next statement irrespective of the success or failure of the previous statement.
Suvendu Shekhar Giri 30-Jul-15 2:14am    
See the solution provided by @OriginalGriff.

You can't ignore an error and continue - but you could use a TRY...CATCH block round each ALTER statement. Use a SELECT to report a problem (so you can deal with it in your code) and then move on to the next TRY...ALTER...CATCH block.
https://msdn.microsoft.com/en-GB/library/ms175976.aspx[^]
 
Share this answer
 
Comments
Suvendu Shekhar Giri 30-Jul-15 1:53am    
What you have suggested will definitely work as you have described but, doesn't GO supposed to ignore the success or failure of a batch and moves to the next batch automatically ?
OriginalGriff 30-Jul-15 2:09am    
GO will work in SSMS - but not in SQL as executed by C# (as the OP is doing):
https://msdn.microsoft.com/en-us/library/ms188037.aspx
Suvendu Shekhar Giri 30-Jul-15 2:12am    
True :)
5ed. Perfect answer then.
TarunKumarSusarapu 30-Jul-15 2:24am    
--------------------------RUN THIS SCRIPT IF GRADEDDETAILS TABLE CONTAINS DATA / IF ERROR OCCURS WHILE CONVERTING THE ABOUVE SRIPT---------
ALTER TABLE GradedDETAILS ADD STOCK_DATE1 DATETIME NULL ----STEP 1--

ALTER TABLE GradedDETAILS ALTER COLUMN [STOCK_DATE] NVARCHAR(100) ----STEP 2--

UPDATE GradedDETAILS SET [STOCK_DATE]=CONVERT(DATETIME, [STOCK_DATE], 104) ----STEP 3--

UPDATE GradedDETAILS SET [STOCK_DATE1]=[STOCK_DATE] ----STEP 4--

ALTER TABLE GradedDETAILS DROP COLUMN [STOCK_DATE] ----STEP 5--

SP_RENAME 'GradedDETAILS.STOCK_DATE1','STOCK_DATE','COLUMN' ----STEP 6--

I want to execute query step by step in sql using C# with single click

if you execute it once then i got error like this

Incorrect syntax near 'SP_RENAME'.
TarunKumarSusarapu 30-Jul-15 2:15am    
Yeah I want the answer for that i want to execute it line by line should i use any condition in front of each query
You can directly use TRY... CATCH in C# and if one of the query is failed then catch the exception in catch and don't mention anything in the catch.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900