如何在MS SQL中的Insert查询中添加以下代码?

时间:2014-03-12 13:01:27

标签: sql sql-server select insert

我想在插入查询中添加此代码.............

 DECLARE @Upper INT;
DECLARE @Lower INT;
SET @Lower = 1   
SET @Upper = 613


DECLARE @R INT = (SELECT @Lower + CONVERT(INT, (@Upper-@Lower+1)*RAND()))  

;WITH MyTable AS (
            SELECT (ROW_NUMBER()OVER (ORDER BY MyTable.SomeFiled)) as row,*
            FROM MyTable)
        SELECT * FROM rat.MLTFQuestion WHERE MyTable= @R   

1 个答案:

答案 0 :(得分:0)

CTE table只是一个临时表,因此您可以这样使用它:

;WITH MyTable AS (
            SELECT (ROW_NUMBER()OVER (ORDER BY MyTable.SomeFiled)) as row,*
            FROM MyTable)

        insert into newtable(row, col2, col3)
        SELECT row,col2,col3 FROM MyTable