Question

I wrote the following query which I hope it will help explaining what I am trying to do exactly, I want to generate 5 records for each record I have in the table:

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

The above query will return the following:

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

based on that record, I want to create 5 agentIDs, that starts with and A and ends with 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

The dollar amount is a fixed value based on what the AgentID field ends with.

Was it helpful?

Solution 2

Here's one way...

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

OTHER TIPS

try this

    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 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top