2013-07-18 116 views
0

我想根據列值生成序列號。我想要有這種輸出。我該怎麼在C#.net的winform以此爲GridView的輸出基於列值生成序列號

TABLE1 

ID  Name  NoStub 
1  arte  3 
2  gonzake  2 


TABLE2 

ID  Name  StubNumberStart   StubNumberEnd 
1  arte   0001      0003 
2  gonzake  0004      0005 
+0

可以在 http://stackoverflow.com/questions/7753080/select-row-n-times在這裏找到答案-with-sql http://stackoverflow.com/questions/1209395/how-to-retrieve-rows-multiple-times-in-sql-server http://stackoverflow.com/questions/2472662/make -sql-select-same-row-multiple-times http://stackoverflow.com/questions/6608055/selecting-the-same-row-multiple-times –

回答

0

嘗試此查詢..它會給結果從表2〜1

DECLARE @T1 AS TABLE (ID INT, NAME VARCHAR(50), STUBNUMBER VARCHAR(10)) 
INSERT INTO @T1 VALUES (1, 'ARTE', '001') 
INSERT INTO @T1 VALUES (1, 'ARTE', '002') 
INSERT INTO @T1 VALUES (1, 'ARTE', '003') 

INSERT INTO @T1 VALUES (1, 'GONZAKE', '004') 
INSERT INTO @T1 VALUES (1, 'GONZAKE', '005') 



SELECT * FROM @T1 

SELECT DISTINCT ID ,NAME, COUNT(*) AS NOSTUB FROM @T1 
GROUP BY ID, NAME 

如果您的請求是表1中不同的表2那麼請讓我知道..你會得到新的查詢......

0
ALTER PROCEDURE ExpandIt 
AS 
BEGIN 
SET NOCOUNT ON; 

DECLARE @Id int; 
DECLARE @name varchar(50); 
DECLARE @noStub int; 

DECLARE @stubNumber char(8); 
DECLARE @count as int = 0; 
DECLARE @continuedID as int = 0; 

DECLARE t1 CURSOR FAST_FORWARD FOR 
SELECT ID, Name,NoStub from Table1 

OPEN t1 
FETCH NEXT FROM t1 INTO @Id, @name, @noStub 

WHILE @@FETCH_STATUS = 0 
BEGIN 
    WHILE (@count < @noStub) 
    BEGIN 
    SET @count = @count + 1; 
    SET @stubNumber = ('0000' + CONVERT (CHAR, @continuedID + @count)); 
    SET @stubNumber = SUBSTRING (@stubNumber,LEN(@stubNumber)-4+1, 4); 

    INSERT INTO Table2 (ID, Name, StubNumber) 
    VALUES (@Id, @name,@stubNumber); 
    END 
    SET @continuedID = @count; 
    SET @count = 0; 
    FETCH NEXT FROM t1 INTO @Id, @name, @noStub 
END 

CLOSE t1 ; 
DEALLOCATE t1 
END 
+0

你可以再次提供一個答案,因爲我編輯它。謝謝 – loypek