Hi, I would like to alter a sql view from C#, Ado.net. I am using frontend as c# and backend as SQL Server.
I tried below options:
option#1: alter view vw_employeesbyId as select ename,location,email from dbo.employees where empid = @empid
option#2:
var sql = @"alter view vw_employeesbyId as select ename,location,email from dbo.employees where empid = @empid ";
Exec sp_ExecuteSql (sql)
Option3:
Drop the view and create view from c#
all 3 options not worked. any other thougths are appreciated
Thanks,
Sreenivasa Rao K
sreenivasa kPosted May 21, 2023, 3:28 AM
I am getting error - ALTER VIEW must be the first statement in a query batch.
sreenivasa kPosted May 21, 2023, 2:36 AM
Thanks for responses. Hi Azarudeen , i had tried one the same option which you provided. but getting incorrect syntax near view.
Please let me know if you have any other thoughts.
Mudzakkir TohaPosted May 21, 2023, 1:08 AM
Do you understand about view in SQL Server?
You are about changing DDL and I guess you need DML.
Do not changing the View on the fly if you not need it, it will causing impact on another users.
This is the view.
This is your DML:
Why you need to re-create view? when you need to get data by EmpId?
Mohamed Azarudeen ZPosted May 21, 2023, 12:31 AM
Hi Srini
Try this
SqlConnectionobject.SqlCommandobject with the ALTER VIEW statement to modify the view.SqlCommandobject if you need to pass dynamic values.ExecuteNonQuerymethod of theSqlCommandobject.SqlConnectionobject once you're done.Dont forget to replace the connection strings, Hope this helps