久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

無法使用 System.Data.SqlClient 在 SQL Server 中創(chuàng)建多個(gè)

Unable to Create Multiple User Defined Functions in SQL Server Using System.Data.SqlClient(無法使用 System.Data.SqlClient 在 SQL Server 中創(chuàng)建多個(gè)用戶定義的函數(shù))
本文介紹了無法使用 System.Data.SqlClient 在 SQL Server 中創(chuàng)建多個(gè)用戶定義的函數(shù)的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試從同一個(gè) .sql 文件中創(chuàng)建多個(gè)用戶定義的函數(shù).我正在使用 SQL Server 并使用 C# 的 System.Data 中的 SqlClient 執(zhí)行我的查詢.

I am trying to create multiple user defined functions from within the same .sql file. I am using SQL Server and am executing my queries using the SqlClient from C#'s System.Data.

.sql 文件的內(nèi)容:

Contents of the .sql file:

CREATE FUNCTION [dbo].[GetUserId] (@username VARCHAR(32))
RETURNS INT
AS
BEGIN
    DECLARE @userId INT = -1

    SET @userId = (SELECT DISTINCT UserId FROM Users WHERE UserName = @username)

    RETURN @userId
END
GO

CREATE FUNCTION [dbo].[GetUserId2] (@username2 VARCHAR(32))
RETURNS INT
AS
BEGIN
    DECLARE @userId2 INT = -1

    SET @userId2 = (SELECT DISTINCT UserId FROM Users WHERE UserName = @username2)

    RETURN @userId2
END

這是我執(zhí)行語句時(shí)拋出的錯(cuò)誤:

Here's the error that is thrown when I execute the statement:

System.Data.SqlClient.SqlException: 'GO' 附近的語法不正確.
必須聲明標(biāo)量變量@username2".
'END' 附近的語法不正確.

System.Data.SqlClient.SqlException: 'Incorrect syntax near 'GO'.
Must declare the scalar variable "@username2".
Incorrect syntax near 'END'.'

有什么想法嗎?總的來說,我是 SQL 的新手,但這對我來說似乎是缺乏對語法/批處理的理解.

Any ideas? I'm new to SQL in general but this seems to be a lack of understanding syntax/batching to me.

我注意到GO"是 SQL Server Management Studio 的一部分,而不是 SqlClient.如果我從 .sql 文件中刪除GO",則會(huì)收到此錯(cuò)誤:

It has come to my attention that 'GO' is part of SQL Server Management Studio, and not the SqlClient. If I remove the 'GO' from my .sql file, then I get this error:

'CREATE FUNCTION' 必須是查詢批處理中的第一條語句.

'CREATE FUNCTION' must be the first statement in a query batch.

如何在不使用GO"的情況下分隔 CREATE FUNCTION 語句?

How do I separate CREATE FUNCTION statements without using 'GO'?

推薦答案

您不能在單個(gè)語句中運(yùn)行多個(gè)批處理.

You cannot run multiple batches in a single statement.

我建議您使用 GO 拆分 TSQL 語句,然后逐個(gè)執(zhí)行批處理.

I would suggest you to split your TSQL statement using GO and then execute the batches one by one.

string multipleUDFs = "CREATE FUNCTION... " +
"GO" + 
"CREATE FUNCTION ";
List<string> statementsToExecute = multileUDFs.Split("GO").ToList();

// Create the command 
var command = new SqlCommand(myConnection);

foreach(string sqlcommand in statementsToExecute)
{

// Change the SQL Command and execute
command.CommandText = sqlcommand;
command.ExecuteNonQuery();
}

這篇關(guān)于無法使用 System.Data.SqlClient 在 SQL Server 中創(chuàng)建多個(gè)用戶定義的函數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運(yùn)算符字符串中刪除重復(fù)項(xiàng))
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 91久久 | 成人免费网视频 | 亚洲综合大片69999 | 亚洲国产一区在线 | 黄页网址在线观看 | 99热这里有精品 | 日韩中文字幕 | 成人久久视频 | 久草在线青青草 | 成人精品在线观看 | 久久婷婷国产麻豆91 | 亚洲精品国产一区 | 九七午夜剧场福利写真 | 99视频久| 日韩中文字幕一区二区 | 99色综合| 国产午夜精品一区二区三区四区 | 亚洲国产精品激情在线观看 | 欧美在线亚洲 | 老熟女毛片 | 狠狠骚 | 激情毛片 | 蜜臀网| 在线日韩福利 | 久优草 | 99日韩| www.99精品| 欧美精品一区二区免费 | 国产在线精品一区二区三区 | 日本一本视频 | 国产精品久久国产愉拍 | 天天爽天天操 | 亚洲在线免费 | 亚洲网站免费看 | 国产精品揄拍一区二区 | 欧美一级特黄aaa大片在线观看 | 久久这里只有精品首页 | 天天操狠狠操 | 91亚洲视频在线 | 久草热线 | 亚洲国产18|