我正在编写一个存储过程来从 TableA 中获取记录列表并将这些记录插入到 TableB 中。现在我面临这个问题。
这是我的 sp :
USE [Sample_DB]
GO
SET ANSI_NULLS ON
GOenter code here
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddStoredProcedure]
AS
BEGIN
DECLARE @ProcessedData AS TABLE (Name varchar(200),MaritalStatus varchar(200))
INSERT INTO @ProcessedData (Name ,MaritalStatus )
SELECT Name,MaritalStatus from TableA where MaritalStatus='Male'
INSERT INTO [TableB]
(
Id
,Name
,MaritalStatus
)
SELECT ('M_'+cast(RIGHT(Year(getDate()),2) as varchar)+'_'+cast(REPLACE(STR((select MAX(Id)+1 from [TableA] where MaritalStatus='Male'),4),' ','0') as varchar)),Name,MaritalStatus FROM @ProcessedData
END
TableA contains,
Id Name MaritalStatus
1 John Male
2 Sam Male
3 Seema Female
当我执行存储过程时,AddStoredProcedure
我得到的输出为
TableB contains,
Id Name MaritalStatus
M_13_0003 John Male
M_13_0003 Sam Male
但我需要如下;
TableB should be,
Id Name MaritalStatus
M_13_0003 John Male
M_13_0004 Sam Male
预先感谢大家的回复。