Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 生成5条记录的SQL Server while循环_Sql Server_While Loop_Case - Fatal编程技术网

Sql server 生成5条记录的SQL Server while循环

Sql server 生成5条记录的SQL Server while循环,sql-server,while-loop,case,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

我写了下面的问题,希望它能帮助解释我到底想做什么, 我想为表中的每条记录生成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,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字段结尾的固定值

这里有一个方法

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
试试这个

    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 

美元金额显示为NULL,代理ID不以A开头。这些美元金额是固定值吗?。我的意思是,他们没有改变?。你如何生成它们?是的,先生,它们是固定值。美元金额是一个基于AgentID字段结尾的固定值。我再次编辑了上述代码。请重试该代码,并告诉我它是否有效。