SQL Server while循环,生成5条记录

时间:2014-05-01 02:28:56

标签: sql-server while-loop case

我编写了以下查询,希望它能帮助解释我正在尝试做的事情, 我想为表中的每条记录生成5条记录:

DECLARE @agentName varchar(15) = 'John Smith',
        @agentEffectiveDate date = '4/1/2014',
        @agentLocation varchar(5) = '85226',
        @agentID varchar(7) = '12345'

CREATE TABLE #table
(
ID int IDENTITY(1,1) NOT NULL,
agentName varchar(15) NOT NULL,
agentEffectiveDate date NOT NULL,
agentLocation varchar(5) NOT NULL,
agentID varchar(7) NOT NULL,
dollarAmount money
PRIMARY KEY (agentName, agentID, agentLocation)
)

INSERT INTO #table (agentName, agentEffectiveDate, agentLocation, agentID) 
VALUES (@agentName, @agentEffectiveDate, @agentLocation, @agentID)

SELECT * FROM #table
DROP TABLE #table

以上查询将返回以下内容:

ID  agentName   agentEffectiveDate  agentLocation   agentID dollarAmount
1   John Smith  2014-04-01          85226           12345   NULL

基于该记录,我想创建5个agentID,以A和A开头,以A,G,M,R,Y结尾。

ID  agentName   agentEffectiveDate  agentLocation   agentID dollarAmount
1   John Smith  2014-04-01          85226           A12345A   3.00
2   John Smith  2014-04-01          85226           A12345G   5.00
3   John Smith  2014-04-01          85226           A12345M   8.00
4   John Smith  2014-04-01          85226           A12345R   72.00
5   John Smith  2014-04-01          85226           A12345Y   12.00

美元金额是基于AgentID字段结束的固定值。

2 个答案:

答案 0 :(得分:1)

试试这个

    DECLARE @agentName varchar(15) = 'John Smith',
        @agentEffectiveDate date = '4/1/2014',
        @agentLocation varchar(5) = '85226',
        @agentID varchar(7) = '12345'

DECLARE @nedded_ids VARCHAR(5) = 'AGMRY'
DECLARE @record_count INTEGER= 0;
DECLARE @record_added INTEGER = 1
DECLARE @currrent_agentid VARCHAR(7) = ''
DECLARE @current_dollaramount MONEY

CREATE TABLE #csv (row_id INTEGER,dolar_amount money)
INSERT #csv (row_id,dolar_amount) VALUES (1,3),(2,5),(3,8),(4,72),(5,12)

CREATE TABLE #table
(
ID int IDENTITY(1,1) NOT NULL,
agentName varchar(15) NOT NULL,
agentEffectiveDate date NOT NULL,
agentLocation varchar(5) NOT NULL,
agentID varchar(7) NOT NULL,
dollarAmount money
PRIMARY KEY (agentName, agentID, agentLocation)
)

SET @record_count = CONVERT(VARCHAR,LEN(@nedded_ids))
PRINT 'RECORD COUNT ' + CONVERT(VARCHAR,@record_count)


WHILE (@record_count >= @record_added)
BEGIN


    SET @currrent_agentid = 'A' + @agentID + SUBSTRING(@nedded_ids,@record_added,1)
    PRINT 'CREATING ' + @currrent_agentid

    SET @current_dollaramount = (SELECT dolar_amount FROM #csv WHERE row_id = @record_added)

    INSERT INTO #table (agentName, agentEffectiveDate, agentLocation, agentID,dollarAmount) 
    VALUES (@agentName, @agentEffectiveDate, @agentLocation, @currrent_agentid,@current_dollaramount)

    SET @record_added = @record_added + 1
END


SELECT * FROM #table
DROP TABLE #table
DROP TABLE #csv 

答案 1 :(得分:0)

这是一种方式......

DECLARE @agentName varchar(15) = 'John Smith',
    @agentEffectiveDate date = '4/1/2014',
    @agentLocation varchar(5) = '85226',
    @agentID varchar(7) = '12345'

CREATE TABLE #table
(
ID int IDENTITY(1,1) NOT NULL,
agentName varchar(15) NOT NULL,
agentEffectiveDate date NOT NULL,
agentLocation varchar(5) NOT NULL,
agentID varchar(7) NOT NULL,
dollarAmount money
PRIMARY KEY (agentName, agentID, agentLocation)
)

INSERT INTO #table (agentName, agentEffectiveDate, agentLocation, agentID) 
VALUES (@agentName, @agentEffectiveDate, @agentLocation, @agentID)

DECLARE @table TABLE
    (
    FirstLetter VARCHAR(1) NOT NULL
    ,LastLetter VARCHAR(1) NOT NULL
    ,Value DECIMAL(10,2) NOT NULL
    ,UNIQUE CLUSTERED (FirstLetter,LastLetter,Value)
    )

INSERT INTO @table
VALUES
('A','A',3)
,('A','G',5)
,('A','M',8)
,('A','R',72)
,('A','Y',12)

SELECT
    a.ID
    ,a.agentName
    ,a.agentEffectiveDate
    ,a.agentLocation
    ,b.FirstLetter + a.agentID + b.LastLetter AgentID
    ,b.Value DollarAmount
FROM #table a
INNER JOIN @table b
    ON 1 = 1

DROP TABLE #table