我需要根據2個參數更新一行。到目前爲止,我的查詢將在處理表格時使用MAX作爲問題字段。 我很難將字段限制爲所需的記錄。該參數是@ClientCode和@BillSeq選擇正確的記錄使用max +1插入數據的單個記錄不是表
這是爲表
DECLARE @ClientCode char(4), @BillSeq int, @BillCommentSeq smallint, @Billcomment varchar(7500)
SELECT
@ClientCode = '00NJ'
,@BillSeq = 1
,@BillCommentSeq = (select MAX(BillCommentSeq) +1 from billcomment)
,@Billcomment = (Select convert (varchar,getdate(),10) +' '+ 'Re-invoiced bill adjusting fees to 0')
INSERT into dbo.BillComment VALUES (@ClientCode, @BillSeq, @BillCommentSeq, '' , @Billcomment)
我的預期但下面的查詢工作它不
DECLARE @ClientCode char(4), @BillSeq int, @BillCommentSeq smallint, @Billcomment varchar(7500)
SELECT
@ClientCode = '00NJ'
,@BillSeq = 1
,@BillCommentSeq = (select MAX(BillCommentSeq) +1 from billcomment where clientcode = @ClientCode and billseq = @BillSeq)
,@Billcomment = (Select convert (varchar,getdate(),10) +' '+ 'Re-invoiced bill adjusting fees to 0')
INSERT into dbo.BillComment VALUES (@ClientCode, @BillSeq, @BillCommentSeq, '' , @Billcomment)
作品查詢
我得到這個錯誤:
Cannot insert the value NULL into column 'BillCommentSeq', table 'database.dbo.BillComment'; column does not allow nulls. INSERT fails. The statement has been terminated.
使用該改造的聲明
DECLARE @ClientCode char(4), @BillSeq int, @BillCommentSeq smallint, @Billcomment varchar(7500)
SELECT
@ClientCode = '00NJ'
,@BillSeq = 1
,@BillCommentSeq = (select MAX(BillCommentSeq) from billcomment) +1 where clientcode = @ClientCode and billseq = @BillSeq)
,@Billcomment = (Select convert (varchar,getdate(),10) +' '+ 'Re-invoiced bill adjusting fees to 0')
INSERT into dbo.BillComment VALUES (@ClientCode, @BillSeq, @BillCommentSeq, '' , @Billcomment)
的錯誤是這樣的:
Incorrect syntax near the keyword 'where'.
您使用的是MySQL還是MS SQL Server? (不要標記不涉及的產品。) – jarlh
MySQL中的「我的」不代表「Mycrosoft」。 – tadman