Solved

CREATE OR ALTER not working in FSM?

  • 19 July 2023
  • 7 replies
  • 69 views

Userlevel 4
Badge +9
  • Sidekick (Partner)
  • 59 replies

Hi Experts,

I am not able to use the the create or alter command for views in FSM.
I believe the following SQL is valid and yet throws an error
 

create or alter view request_temp_view as 
select * from request

 

icon

Best answer by SAMLK 21 July 2023, 07:50

View original

7 replies

Userlevel 6
Badge +26

Hi @Miraj 

Try the following

IF OBJECT_ID('dbo.request_temp_view', 'V') IS NOT NULL
DROP VIEW dbo.request_temp_view
GO

CREATE VIEW request_temp_view AS
SELECT * FROM REQUEST

Cheers!

Userlevel 4
Badge +9

@Shneor Cheshin 
 

 

Userlevel 6
Badge +26

Hey @Miraj 

Run each script seperatly.

To make it simple

DROP VIEW request_temp_view

Then

CREATE VIEW request_temp_view AS
SELECT * FROM REQUEST

Cheers!

Userlevel 4
Badge +9

@Shneor Cheshin 

Thanks. yes, running separately works. although, I am trying to use a “create or alter” type of statement to get this to work in one go. To give some context, when we create views and the corresponding metadata for it, we can save the view script as following
 


So when we deliver to another environment, if the sql against this Table Script is present it automatically creates the view. The issue I have is when we make changes to an existing view and update the table script with a new sql. Now, “Create VIEW” no longer works becasue the target environment already has the view, and thus our changes don’t get automatically deployed upon delivery.

This is why I was exploring the option of using “CREATE OR ALTER” so that it would create if the view is not there or alter it if it were there.

Userlevel 6
Badge +26

Hey @Miraj 

I think you are looking for create or replace

CREATE OR REPLACE VIEW request_temp_view  AS
SELECT * FROM REQUEST

Cheers!

Userlevel 4
Badge +12

Hi @Miraj 

Try out this query on your SQL query tool so that the same query must work on the package deployment.

IF OBJECT_ID('c_data_view') IS NULL
BEGIN
EXEC('CREATE VIEW c_data_view AS SELECT TOP 10 * FROM REQUEST')
END
ELSE
BEGIN
EXEC('ALTER VIEW c_data_view AS SELECT TOP 10 * FROM REQUEST')
END

make sure the package deploying user has the EXECUTEDBQUERY, EXECUTEDBEDIT functions enabled under the role

Userlevel 4
Badge +9

Hi @Miraj 

Try out this query on your SQL query tool so that the same query must work on the package deployment.

IF OBJECT_ID('c_data_view') IS NULL
BEGIN
EXEC('CREATE VIEW c_data_view AS SELECT TOP 10 * FROM REQUEST')
END
ELSE
BEGIN
EXEC('ALTER VIEW c_data_view AS SELECT TOP 10 * FROM REQUEST')
END

make sure the package deploying user has the EXECUTEDBQUERY, EXECUTEDBEDIT functions enabled under the role

Thanks mate. This works

Reply