C# MSSQL 更改表然后修改值

发布于 2024-10-02 12:43:12 字数 1042 浏览 3 评论 0原文

虽然以下内容在 SQL Server Management Studio 中工作正常,但在 C# 中不起作用:

DECLARE @PeriodID AS bigint;
SELECT TOP 1 @PeriodID = PeriodID FROM Periods ORDER BY PeriodID DESC;
IF NOT EXISTS(SELECT * FROM information_schema.columns WHERE COLUMN_NAME = N'PeriodID' AND TABLE_NAME = N'MobilePlans')
BEGIN
    BEGIN
    ALTER TABLE MobilePlans ADD PeriodId bigint NULL
    END
    BEGIN
    UPDATE MobilePlans SET PeriodID = @PeriodID
    END
    BEGIN
    ALTER TABLE MobilePlans ALTER COLUMN PeriodID bigint NOT NULL
    END
END

在 C# 中,它不断告诉我 无效的列名 'PeriodID'。 经过几个小时的搜索后,我想我会在这里问。

在搜索时,我遇到了 http://bytes .com/topic/c-sharp/answers/258520-problem-sqlcommand-t-sql-transaction,但我无法真正将我的条件查询转换为该查询。

为什么 C# 不能做与 Management studio 相同的事情?

是否有另一种方法可以在 C# 中执行查询的操作?我需要在 400 个数据库上执行此操作,因此我非常需要一个脚本来为我执行此操作。

提前致谢!

SQL服务器版本是2008。 管理器版本是2008 (10.0.2531)。 .NET框架版本是2.0。

While the following work fine in SQL Server Management Studio, it just won't work in C#:

DECLARE @PeriodID AS bigint;
SELECT TOP 1 @PeriodID = PeriodID FROM Periods ORDER BY PeriodID DESC;
IF NOT EXISTS(SELECT * FROM information_schema.columns WHERE COLUMN_NAME = N'PeriodID' AND TABLE_NAME = N'MobilePlans')
BEGIN
    BEGIN
    ALTER TABLE MobilePlans ADD PeriodId bigint NULL
    END
    BEGIN
    UPDATE MobilePlans SET PeriodID = @PeriodID
    END
    BEGIN
    ALTER TABLE MobilePlans ALTER COLUMN PeriodID bigint NOT NULL
    END
END

In C#, it keeps telling me Invalid column name 'PeriodID'. and after spending a couple of hours searching, I thought I'd ask here.

While searching, I came across http://bytes.com/topic/c-sharp/answers/258520-problem-sqlcommand-t-sql-transaction, but I couldn't really translate my conditional query to that.

Why can't C# do the same as the Management studio?

Is there another way to do what the query does, that works in C#? I need to perform this on 400 databases, so I'd really like a script to do it for me.

Thanks in advance!

SQL server version is 2008.
Manager version is 2008 (10.0.2531).
.NET framework version is 2.0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

镜花水月 2024-10-09 12:43:12

在 Management Studio 中运行它时,我收到“无效的列名称 'PeriodID'”,如果该表还没有 periodID 列

重现:

create table Periods (
    PeriodID bigint not null
)
go
insert into Periods(PeriodID) select 1
go
create table MobilePLans (
    BLah int not null
)
go
insert into MobilePLans(BLah) select 2
go
DECLARE @PeriodID AS bigint;
SELECT TOP 1 @PeriodID = PeriodID FROM Periods ORDER BY PeriodID DESC;
IF NOT EXISTS(SELECT * FROM information_schema.columns WHERE COLUMN_NAME = N'PeriodID' AND TABLE_NAME = N'MobilePlans')
BEGIN
    BEGIN
    ALTER TABLE MobilePlans ADD PeriodId bigint NULL
    END
    BEGIN
    UPDATE MobilePlans SET PeriodID = @PeriodID
    END
    BEGIN
    ALTER TABLE MobilePlans ALTER COLUMN PeriodID bigint NOT NULL
    END
END

原因很简单 - SQL Server 尝试完全编译每个批次。 如果该列已存在,则可以编译 UPDATE 语句。如果没有,您会收到错误。

如果将更新放入 Exec 中:

EXEC('UPDATE MobilePlans SET PeriodID = ' + @PeriodID)

那么它将在该列确实存在时进行编译。

I get "invalid column name 'PeriodID'" running it in Management Studio, if the table doesn't already have the PeriodID column.

Repro:

create table Periods (
    PeriodID bigint not null
)
go
insert into Periods(PeriodID) select 1
go
create table MobilePLans (
    BLah int not null
)
go
insert into MobilePLans(BLah) select 2
go
DECLARE @PeriodID AS bigint;
SELECT TOP 1 @PeriodID = PeriodID FROM Periods ORDER BY PeriodID DESC;
IF NOT EXISTS(SELECT * FROM information_schema.columns WHERE COLUMN_NAME = N'PeriodID' AND TABLE_NAME = N'MobilePlans')
BEGIN
    BEGIN
    ALTER TABLE MobilePlans ADD PeriodId bigint NULL
    END
    BEGIN
    UPDATE MobilePlans SET PeriodID = @PeriodID
    END
    BEGIN
    ALTER TABLE MobilePlans ALTER COLUMN PeriodID bigint NOT NULL
    END
END

The reason is simple - SQL Server tries to compile each batch completely. If the column already exists, then the UPDATE statement can be compiled. If not, you get the error.

If you put the update inside an Exec:

EXEC('UPDATE MobilePlans SET PeriodID = ' + @PeriodID)

Then it will be compiled at the point that the column does exist.

書生途 2024-10-09 12:43:12

我猜你的 c# 代码中有拼写错误。

错误是 PeriodeID,而您的列名称是 PeriodID

I guess you have a spelling mistake in your c# code.

The error is saying PeriodeID whereas your column name is PeriodID.

み零 2024-10-09 12:43:12

说明:

1 - 您想将此脚本推送到许多数据库吗?

2 - 列名“PeriodeID”无效,我看不到名为“PeriodeId”的列,但看到“PeriodId”,这是拼写错误吗?

3 - 您可以在没有主 BEGIN/END 块的情况下尝试相同的块吗?

Clarification:

1 - You would like to push this script it against many databases?

2 - Invalid column name 'PeriodeID', I cannot see a column called "PeriodeId", but "PeriodId", is this a typo?

3 - Can you try the same block without main BEGIN/END block?

鼻尖触碰 2024-10-09 12:43:12

服务器排序规则是否不区分大小写?因为您要添加期间Id,然后更新期间ID

Is the server collation Case Insensitive ? Because you're adding PeriodId and then updating PeriodID

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文