我想循环将数据放入@tbl表的@ tbl2表中。不幸的是我不知道我要插入什么(INSERT INTO @ tbl2值(@ ID,?,?))我的意思是第二和第三个参数。
MSSQL 2008 R2
例如(table @tbl):
SpecId | SP
3 | ' 5/1/2008 8:30:52 AM'
8 | ' 5/1/2008 8:30:52 AM'
9 | ' 5/1/2008 8:30:52 AM'
如果@count = 3,我想获取这些数据(9 |' 5/1/2008 8:30:52 AM')
DECLARE @tbl TABLE
(
SpecId BIGINT,
SP DATETIME
)
DECLARE @tbl2 TABLE
(
ID int,
SpecId BIGINT,
SP DATETIME
)
DECLARE @ID INT
DECLARE @count INT
DECLARE @firstNumber INT
DECLARE @secondNumber INT
SET @firstNumber = @PageSize * 10
SET @secondNumber = @firstNumber - 10
WHILE (@count <= @TotalRowsNumber)
BEGIN
IF @count > @secondNumber and @count <= @firstNumber
BEGIN
SET @ID +=1
INSERT INTO @tbl2 Values(@ID,?,?)
END
SET @count = (@count + 1)
END
答案 0 :(得分:1)
固定。
我在此表中添加了 ID int ,稍后在循环中我为这两个参数创建了简单的选择:
DECLARE @tbl TABLE ( SpecId BIGINT, SP DATETIME )
答案 1 :(得分:1)
你快到了......看看这个:
DECLARE @tbl TABLE
(
Id int identity(1, 1),
SpecId BIGINT,
SP DATETIME
)
DECLARE @tbl2 TABLE
(
ID int,
SpecId BIGINT,
SP DATETIME
)
DECLARE @ID INT
DECLARE @count INT
DECLARE @firstNumber INT
DECLARE @secondNumber INT
declare @PageSize int
declare @TotalRowsNumber int
SET @firstNumber = @PageSize * 10
SET @secondNumber = @firstNumber - 10
-- Populate tbl
insert
into @tbl
select value1, value2
from original_table
set @TotalRowsNumber = @@rowcount
set @count = 1
WHILE (@count <= @TotalRowsNumber)
BEGIN
select @value1 = value1
, @value2 = value2
from @tbl
where Id = @count
IF @count > @secondNumber and @count <= @firstNumber
BEGIN
SET @ID +=1
INSERT INTO @tbl2 Values(@ID,@value1,@value2)
END
SET @count = (@count + 1)
END
我做了什么。 @tbl需要获得一个名为Id的额外列。这是一个标识列,用于在以后的体育场中进行引用。接下来,使用原始值填充@tbl。在while循环中,您将@tbl值转换为某些变量(感谢@count = id循环记录),然后将它们插入@ tbl2