I have some inline SQL Scripts (functions and stored procedures) generated with Entity framework , Code first approach.
我有一些内联SQL脚本(函数和存储过程)是用实体框架、代码优先方法生成的。
Update-Database -Script -SourceMigration:0
- database就- script -SourceMigration:0
With the above command I get SQL Script file that I execute on test or production.
通过上面的命令,我获得在测试或生产中执行的SQL脚本文件。
However I cannot run the generated script because of the following error:
但是,由于以下错误,我无法运行生成的脚本:
'CREATE FUNCTION' must be the first statement in a query batch.
The script is generated as:
该脚本生成如下:
IF @CurrentMigration < '201410150019333_CreatefnGenerateRequestCode' BEGIN CREATE FUNCTION [dbo].[fnGenerateRequestCode] ( @userID varchar(max) )
RETURNS varchar(14)
as
如果@CurrentMigration < '201410150019333_CreatefnGenerateRequestCode' BEGIN CREATE FUNCTION [dbo]。[fnGenerateRequestCode] (@userID varchar(max))返回varchar(14) as
How can I fix this?
我该怎么解决这个问题呢?
2 个解决方案
#1
2
You have to generate your code and execute it as dynamic sql.
您必须生成代码并将其作为动态sql执行。
DECLARE @Sql NVARCHAR(MAX)
SET @Sql =
'
IF OBJECT_ID(''fn_Test'') IS NOT NULL DROP FUNCTION fn_Test
GO
CREATE FUNCTION fn_Test(@a INT)
RETURNS INT
BEGIN
RETURN @a
END
'
IF 1 = 1
BEGIN
EXEC(@Sql)
END
#2
3
You can avoid the
你可以避免
should be the first statement in a batch file
应该是批处理文件中的第一个语句吗
error without adding GO statements by putting the sql inside an EXEC command:
将sql放在EXEC命令中而不添加GO语句的错误:
Sql(EXEC('BEGIN CREATE FUNCTION etc'))
Reference:
参考:
https://*.com/a/20352867/150342
https://*.com/a/20352867/150342
#1
2
You have to generate your code and execute it as dynamic sql.
您必须生成代码并将其作为动态sql执行。
DECLARE @Sql NVARCHAR(MAX)
SET @Sql =
'
IF OBJECT_ID(''fn_Test'') IS NOT NULL DROP FUNCTION fn_Test
GO
CREATE FUNCTION fn_Test(@a INT)
RETURNS INT
BEGIN
RETURN @a
END
'
IF 1 = 1
BEGIN
EXEC(@Sql)
END
#2
3
You can avoid the
你可以避免
should be the first statement in a batch file
应该是批处理文件中的第一个语句吗
error without adding GO statements by putting the sql inside an EXEC command:
将sql放在EXEC命令中而不添加GO语句的错误:
Sql(EXEC('BEGIN CREATE FUNCTION etc'))
Reference:
参考:
https://*.com/a/20352867/150342
https://*.com/a/20352867/150342