简体   繁体   English

有关SQL Server中SQL插入的查询?

[英]Query regarding SQL Insert in SQL Server?

I am using SQL Server 2008 and developing a project which is in maintenance phase. 我正在使用SQL Server 2008并正在开发处于维护阶段的项目。

I want to insert record in a table whose primary key is an Integer but not an identity. 我想在主键为Integer而不是Identity的表中插入记录。 eg table name is tblFiles and fields are ID, FileName, FileContent . 例如,表名是tblFiles ,字段是ID, FileName, FileContent

Actually that table is in use so I don't want to make any schema change in it. 实际上,该表正在使用中,因此我不想在其中进行任何模式更改。 And I want the key after row insertion because I have to put that in another table. 而且我想在行插入后添加键,因为我必须将其放在另一个表中。 Existing values in the Id column are different integer, means not in sequence. Id列中的现有值是不同的整数,表示不按顺序。

So I want the query that also returns me the Id value. 因此,我希望查询也返回ID值。 So I want to insert only FileName and FileContent and some sort of sql to whom I can embed in my insert query which insert a unique Id and also send me that id 所以我只想插入FileNameFileContent以及某种SQL,可以将其插入到我的插入查询中,从而插入一个唯一ID并向我发送该ID

Well, if it's not an IDENTITY field - don't you already have to specify the "ID" in your insert for it to succeed ? 好吧,如果它不是IDENTITY字段-您是否不必在插入中指定“ ID”才能成功? If so - you already have the ID! 如果是这样-您已经 ID! Or what am I missing? 还是我想念什么? Is the ID determined by a trigger or something?? ID是由触发器或其他因素确定的吗?

If so, try this query: 如果是这样,请尝试以下查询:

INSERT INTO dbo.tblFiles(FileName, FileContent)
OUTPUT inserted.ID
VALUES ('yourfile.name', 'your contents')

This should return the newly inserted ID from the INSERT query. 这应该从INSERT查询返回新插入的ID。

Marc

You could create a unique integer, not so elegantly, using 您可以使用以下方法创建一个唯一的整数(不是那么优雅)

SELECT MAX(ID) + 1 FROM tblFiles 从tblFiles中选择SELECT MAX(ID)+1

And simply return this from your query or sproc as the case maybe. 并视情况从查询或sproc中返回此值。 Otherwise follow as marc_s says if it is known already. 否则,请按照marc_s所说的(如果已经知道)进行操作。

UPDATE: have to say, rather than this fudge as requested, I would strongly recommend pushing back hard and getting table changed so this is an identity column, as this is what is. 更新:不得不说,与其强烈要求,不如我不建议这样做,我强烈建议您加倍努力并更改表,因此这是一个标识列,因为这就是事实。 all answers so far are simply fudges, mine especially. 到目前为止,所有答案都是软糖,特别是我的。

Change the Columns Identity Specification > Is Identity to Yes. 将“身份标识列”>“身份标识”更改为“是”。

The after inserting into the table you can 将表插入后即可

Select SCOPE_IDENTITY() 

to get the integer that was just added and return this in your SP. 获取刚刚添加的整数,然后在您的SP中返回该整数。

If you really can't edit the database schema maybe you could add another table to the database that has two columns called ID and CurrentDate. 如果您确实无法编辑数据库架构,则可以将另一个表添加到数据库中,该表具有两列,分别称为ID和CurrentDate。 Make the ID column an Identity. 将ID列设为身份。 In your code insert into this table first select SCOPE_IDENTITY() and then use the integer returned to insert as the ID in your tblFles table. 在将代码插入此表中时,首先选择SCOPE_IDENTITY(),然后使用返回的整数作为ID插入tblFles表中。

PS Stop prefixing your table with tbl that's so 1999. :) PS停止在表前加上tbl这样的前缀1999::)

so my final query look like... 所以我的最终查询看起来像...

Insert into dbo.tblData (Id, FName, LName) 
output inserted.Id
values ((SELECT MAX(ID) + 1 FROM dbo.tblData), 'xyz', 'abc')

We can assign max(ID)+1 in to an integer variable, then we can Insert 我们可以将max(ID)+1输入一个整数变量,然后我们可以插入

Declare @ID int
Select @ID = ISNULL(MAX(ID),0) + 1 FROM tblFiles

INSERT INTO tblFiles 
(
  ID, FileName, FileContent
)
Select @ID,'FileName','FileContent'

This insertion is direct, 这种插入是直接的,

INSERT INTO tblFiles 
(
  ID, FileName, FileContent
)
 Select (Select ISNULL(MAX(ID),0) + 1 FROM tblFiles),'FileName','FileContent'

Here we have to use ISNULL condition because there is no data in table then it will return Null. 这里我们必须使用ISNULL条件,因为表中没有数据,那么它将返回Null。 So ISNULL(MAX(ID),0) + 1 this condition will give Data is null then 0+1=1. 因此ISNULL(MAX(ID),0)+ 1此条件将给出Data为null,然后0 + 1 = 1。

Thank you 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM