Getting error in SQL query with function use

南笙酒味 提交于 2020-01-29 17:55:27

问题


When I run this query through C# adapter it is causing an error:

Incorrect syntax near Use

Any ideas? When I run this in SQL Server 2008 R2 it's working fine.

create FUNCTION [dbo].[fn_Split] (@sep nvarchar(10), @s nvarchar(4000))
RETURNS table
AS
  RETURN (
  WITH Pieces(pn, start, stop) AS (
  SELECT 1, 1, CHARINDEX(@sep, @s)
  UNION ALL
  SELECT pn + 1, stop + (datalength(@sep)/2), 
    CHARINDEX(@sep, @s, stop + (datalength(@sep)/2))
  FROM Pieces
  WHERE stop > 0
)
SELECT pn,
SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END) AS value
FROM Pieces
)
;

/****** Object: Table [dbo].[drillDowntable1] Script Date: 09/24/2012 18:43:32 ******/
USE [master]

SET ANSI_NULLS ON
;
SET QUOTED_IDENTIFIER ON
;
SET ANSI_PADDING ON
;
CREATE TABLE [dbo].[drillDowntable1](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](20) NULL,
[json] [varchar](max) NULL,
[isActive] [bit] NOT NULL
) ON [PRIMARY]

回答1:


You need a GO to put things in their own batch.

FROM Pieces
)
;

GO -- < this is important
/****** Object: Table [dbo].[drillDowntable1] Script Date: 09/24/2012 18:43:32 ******/
USE [master]


来源:https://stackoverflow.com/questions/12623142/getting-error-in-sql-query-with-function-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!