'创建视图'必须是查询批处理中的第一个语句。 (尽管GO声明)

时间:2017-04-25 14:51:08

标签: sql sql-server entity-framework

背景

为了使视图保持最新,我希望将每个视图保留为.sql文件夹中的Views文件。在Seed()方法上,我正在遍历这些文件并执行SQL。

我希望这个SQL能够删除并重新创建视图。

设置

名为V_DW_PrepaymentReport.sql的视图文件:

if exists(select 1 from sys.views where name='V_DW_PrepaymentReport' and type='V')
DROP VIEW V_DW_PrepaymentReport

GO

create view V_DW_PrepaymentReport as

...(other SQL here to finish creating the view)

种子方法代码(不太相关,但只是FYI):

    private void RegenerateDatabaseViews(RsDatabase context)
    {

        var viewsDirectory = $"{AppDomain.CurrentDomain.BaseDirectory}Views";

        var allViewFiles = Directory.GetFiles(viewsDirectory, "*.sql").OrderBy(x=>x);

        if (!allViewFiles.Any()) { throw new Exception($"No view files found in {viewsDirectory}");}

        foreach (var viewFile in allViewFiles)
        {
            context.Database.ExecuteSqlCommand(File.ReadAllText(viewFile));
        }
    }

问题

运行Update-Database时,按下Seed方法并运行该文件后,我看到:

  

' GO'

附近的语法不正确      

'创建视图'必须是查询批处理中的第一个语句。

问题

尽管在批次之间使用GO语句,为什么我会看到此错误? SQL服务器是否忽略该关键字并将整个文件视为一个批处理?

1 个答案:

答案 0 :(得分:4)

04-25 10:34:40.514 7810-7866/com.ferretotalapp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3 Process: com.ferretotalapp, PID: 7810 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:309) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.NoClassDefFoundError: com.google.android.gms.maps.GoogleMapOptions at com.airbnb.android.react.maps.AirMapManager.<init>(AirMapManager.java:52) at com.airbnb.android.react.maps.MapsPackage.createViewManagers(MapsPackage.java:39) at com.facebook.react.XReactInstanceManagerImpl.createAllViewManagers(XReactInstanceManagerImpl.java:699) at com.facebook.react.CoreModulesPackage.createUIManager(CoreModulesPackage.java:206) at com.facebook.react.CoreModulesPackage.access$200(CoreModulesPackage.java:71) at com.facebook.react.CoreModulesPackage$8.get(CoreModulesPackage.java:145) at com.facebook.react.CoreModulesPackage$8.get(CoreModulesPackage.java:142) at com.facebook.react.LazyReactPackage.createNativeModules(LazyReactPackage.java:76) at com.facebook.react.XReactInstanceManagerImpl.processPackage(XReactInstanceManagerImpl.java:958) at com.facebook.react.XReactInstanceManagerImpl.createReactContext(XReactInstanceManagerImpl.java:863) at com.facebook.react.XReactInstanceManagerImpl.access$600(XReactInstanceManagerImpl.java:110) at com.facebook.react.XReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(XReactInstanceManagerImpl.java:214) at com.facebook.react.XReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(XReactInstanceManagerImpl.java:193) at android.os.AsyncTask$2.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)  at java.lang.Thread.run(Thread.java:818)  04-25 10:34:40.536 7810-7835/com.ferretotalapp W/unknown:InspectorPackagerConnection: Couldn't connect to packager, will silently retry<br> <br> 04-25 10:34:40.714 7810-7844/com.ferretotalapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xf3e145a0 命令是client tools的命令,而不是SQL Server的命令。每个客户端工具都会将文本拆分为批次,并将每个批次分别提交给服务器。

好吧,猜猜看 - 你现在正在使用SQL脚本并希望GO行为 - 所以你现在正在实现一个客户端工具,这取决于你< / em>实现相同的行为。

此外,T-SQL中不存在GO