sql-source-control icon indicating copy to clipboard operation
sql-source-control copied to clipboard

Error: 'CREATE VIEW' must be the first statement in a query batch.

Open DanielOverdevest opened this issue 7 years ago • 9 comments

Type

What kind of issue is this?

[x] Bug report.
[ ] Feature request.

Current Behavior

It's not possible to deploy a view into another database, using ssc push production Gives this error code:

Error: 'CREATE VIEW' must be the first statement in a query batch.

It looks like the current situation doesn't allow to run two statements in a single batch.

Example view:

if exists (select * from sys.objects where object_id = object_id('[sys].[database_firewall_rules]') and type = 'V')
drop view [sys].[database_firewall_rules]
go

CREATE VIEW sys.database_firewall_rules AS SELECT id, name, start_ip_address, end_ip_address, create_date, modify_date FROM sys.database_firewall_rules_table

Expected behavior

Expected to create the view for me.

Steps to Reproduce

Environment: Azure SQL Database (latest version) ssc pull development ssc push production

Other Information

DanielOverdevest avatar Sep 18 '18 13:09 DanielOverdevest

@justinlettau do you think this is a bug? I still get the error using SSC PUSH production. I think it's because it starts with 'if exists' .... then GO.. And on fifth line it have the CREATE VIEW. Do you have already a solution for this?

DanielOverdevest avatar Oct 19 '18 09:10 DanielOverdevest

I found out that I can change the IF exists lines and CREATE VIEW with 'CREATE OR ALTER VIEW'. This works fine for me!

DanielOverdevest avatar Oct 19 '18 09:10 DanielOverdevest

@DanielOverdevest Yeah, I do think it's a bug. I believe it has to do with how the queries are executed on push. I'm glad the CREATE OR ALTER works for you. In addition to fixing this bug, i'd like to add CREATE OR ALTER as an idempotency option.

justinlettau avatar Dec 12 '18 22:12 justinlettau

@DanielOverdevest Can you reproduce this with in the latest version? The push command is designed to separate statements by the GO and execute each segment separately; So in theory this error shouldn't occur.

justinlettau avatar Jan 16 '19 21:01 justinlettau

Hi Justin, I will, next week. Ok? GR. Daniel

On 16 Jan 2019, at 22:42, Justin Lettau [email protected] wrote:

@DanielOverdevest https://github.com/DanielOverdevest Can you reproduce this with in the latest version? The push command is designed to separate statements by the GO and execute each segment separately; So in theory this error shouldn't occur.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/justinlettau/sql-source-control/issues/42#issuecomment-454954057, or mute the thread https://github.com/notifications/unsubscribe-auth/ARsGeDRlFmKer0WNg4-E816pk2jXdPFQks5vD5y2gaJpZM4Wt8nZ.

DanielOverdevest avatar Jan 18 '19 10:01 DanielOverdevest

@DanielOverdevest Thanks!

justinlettau avatar Jan 18 '19 21:01 justinlettau

Hi Justin,I installed the latest version (2.1.0 and 2.1.1) and both are not working anymore with Azure SQL database (basic tier). I think you’re using functions which are not implemented in that version of SQL server.If you can give me some hints where to look at, I’m willing to dive a bit deeper in the problem and test for a solution.GR.Daniel

DanielOverdevest avatar Jan 25 '19 19:01 DanielOverdevest

@DanielOverdevest The only issue I am aware of with Azure SQL is regarding jobs (#65). Is the error your getting look related to jobs? If so, this config should address it:

{
 "output": {
   "jobs": false
 }
}

If not, could you provide some details about the error you are seeing? Thanks.

justinlettau avatar Jan 25 '19 20:01 justinlettau

I tried it out with the latest version (I had to wait until I need to push anything into production). I still get the same error: ✖ RequestError: 'CREATE VIEW' must be the first statement in a query batch.

When I find time I'm gonna change the pull request which fixed this problem using this statement: CREATE OR ALTER VIEW

Thanks for the 2.1.1. update by the way. Clustered Indexes of multiple columns are now better captured.

UPDATE: changing it to CREATE OR ALTER also returns the same error. So solution must be found in the way it't sent to the server and GO's, what you already suggested.

DanielOverdevest avatar Feb 01 '19 08:02 DanielOverdevest