我需要通过存储过程将表单中的数据提交到SQL2005数据库。困难的部分是我还需要从辅助表中获取5个随机记录,并将这些记录作为表1中插入记录的一部分插入。
我的结构类似于:
Tbl_Organisations(从中获取5条随机记录的表格)
Tbl_Campaigns(要插入的表格)
键|名字|日期| organisation_1 | organisation_2 | organisation_3 |等........
我需要从'tbl_Organisations'获取5个唯一/随机记录,捕获它们并将它们与输入到存储过程的数据一起插入到单个记录中。我理解数组不是SQL2005(?)中的一个选项。
那么我如何捕获这些记录,然后将它们作为单个记录与SP输入一起插入?
我将非常感谢任何帮助,因为我正在努力掌握SQL的复杂性
感谢。
答案 0 :(得分:0)
可以通过在查询结尾处将ORDER BY NEWID()
添加到第二个表来拉取随机记录。当然,在开头使用TOP 5
只返回5行。
然后在返回的行中使用SQL游标循环来填充将在第一个表的最终插入中使用的5个静态变量(@var1
,@var2
等)。您将不得不使用循环计数器和一些CASE
语句来确定每个运行循环的变量。最后,请确保处理返回少于5行的情况,如果可以使用您的数据集。
答案 1 :(得分:0)
如果你有一个顺序数字键(如身份),并且从不删除任何行或回滚任何事务,这应该工作。你没有发布很多细节,所以这只是一个猜测,你可以根据你的代码。如果删除行,则可以使用ROW_NUMBER()使类似方法起作用。
来自here,试试这个:
DECLARE @Random1 INT;
DECLARE @Random2 INT;
DECLARE @Random3 INT;
DECLARE @Random4 INT;
DECLARE @Random5 INT;
DECLARE @Upper INT;
DECLARE @Lower INT
---- This will create a random number between 1 and count() of table
SET @Lower = 1 ---- The lowest random number
SELECT @Upper= COUNT(*) FROM Tbl_Organisations ---- The highest random number
SELECT @Random1 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random2 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random3 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random4 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
SELECT @Random5 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
INSERT INTO Tbl_Campaigns (...., organisation_1,organisation_2,organisation_3...)
SELECT
...., t1.organisation_name, t2.organisation_name,t3.organisation_name...
FROM Tbl_Organisations t1
INNER JOIN Tbl_Organisations t2 ON t2.key=@Random2
INNER JOIN Tbl_Organisations t3 ON t3.key=@Random3
INNER JOIN Tbl_Organisations t4 ON t4.key=@Random4
INNER JOIN Tbl_Organisations t5 ON t5.key=@Random5
WHERE t1.key=@Random1
编辑基于需要使用row_number()
试试这段代码:
--set up tables
DECLARE @Tbl_Organisations table (O_KeyID int not null primary key identity(1,1), organisation_name varchar(20) not null)
DECLARE @Tbl_Campaigns table (C_KeyID int not null primary key identity(1,1), Cname varchar(10), createdate datetime, organisation_1 int, organisation_2 int, organisation_3 int, organisation_4 int, organisation_5 int)
--set up data
INSERT INTO @Tbl_Organisations VALUES ('one')
INSERT INTO @Tbl_Organisations VALUES ('two')
INSERT INTO @Tbl_Organisations VALUES ('three')
INSERT INTO @Tbl_Organisations VALUES ('pine')
INSERT INTO @Tbl_Organisations VALUES ('oak')
INSERT INTO @Tbl_Organisations VALUES ('maple')
INSERT INTO @Tbl_Organisations VALUES ('car')
INSERT INTO @Tbl_Organisations VALUES ('train')
INSERT INTO @Tbl_Organisations VALUES ('boat')
INSERT INTO @Tbl_Organisations VALUES ('dog')
INSERT INTO @Tbl_Organisations VALUES ('cat')
INSERT INTO @Tbl_Organisations VALUES ('horse')
INSERT INTO @Tbl_Organisations VALUES ('square')
INSERT INTO @Tbl_Organisations VALUES ('triangle')
INSERT INTO @Tbl_Organisations VALUES ('circle')
--temp areas to hold the 5 random numbers
DECLARE @Random1 INT;
DECLARE @Random2 INT;
DECLARE @Random3 INT;
DECLARE @Random4 INT;
DECLARE @Random5 INT;
--temp areas to hold the ranges to generate random numbers within
DECLARE @Upper INT;
DECLARE @Lower INT
DECLARE @range int
--get values to determine ranges of random numbers,
SET @Lower = 1 ---- The lowest random number
SELECT @Upper= COUNT(*) FROM @Tbl_Organisations ---- The highest random number
SET @Range=(@Upper - @Lower)/5
--will divide the @Tbl_Organisations table in to 5 ranges and select a random number from each range
--set the lower and upper limit on range 1
SELECT @Upper=@Lower+@Range
PRINT '@Lower='+CONVERT(varchar(50),@Lower)+', @Upper='+CONVERT(varchar(50),@Upper)
--get a random value from range 1
SELECT @Random1 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
--set the lower and upper limit on range 2
SELECT @Lower=@Upper+1,@Upper=@Lower+@Range
PRINT '@Lower='+CONVERT(varchar(50),@Lower)+', @Upper='+CONVERT(varchar(50),@Upper)
--get a random value from range 2
SELECT @Random2 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
--set the lower and upper limit on range 3
SELECT @Lower=@Upper+1,@Upper=@Lower+@Range
PRINT '@Lower='+CONVERT(varchar(50),@Lower)+', @Upper='+CONVERT(varchar(50),@Upper)
--get a random value from range 3
SELECT @Random3 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
--set the lower and upper limit on range 4
SELECT @Lower=@Upper+1,@Upper=@Lower+@Range
PRINT '@Lower='+CONVERT(varchar(50),@Lower)+', @Upper='+CONVERT(varchar(50),@Upper)
--get a random value from range 4
SELECT @Random4 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
--set the lower and upper limit on range 5
SELECT @Lower=@Upper+1,@Upper=COUNT(*) FROM @Tbl_Organisations
PRINT '@Lower='+CONVERT(varchar(50),@Lower)+', @Upper='+CONVERT(varchar(50),@Upper)
--get a random value from range 5
SELECT @Random5 = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)
--this uses a CTE names "RowNumbers" to enumerate @Tbl_Organisations with a column "RowNumber" that is a unique sequential continueous from 1 to count(*) of @Tbl_Organisations
;with RowNumbers AS
(
SELECT O_KeyID, row_number() over(order by O_KeyID) AS RowNumber from @Tbl_Organisations
)
--one row will be inserted, using the 5 joined in @Tbl_Organisations
INSERT INTO @Tbl_Campaigns (Cname,createdate, organisation_1, organisation_2, organisation_3, organisation_4, organisation_5)
--this will select and combine the 5 rows into 1 row, using the rownumber joined to the variables with the random values
SELECT
'YourName'
,getdate()
,t1.O_KeyID --could be t1.organisation_name
,t2.O_KeyID --could be t2.organisation_name
,t3.O_KeyID --could be t3.organisation_name
,t4.O_KeyID --could be t4.organisation_name
,t5.O_KeyID --could be t5.organisation_name
from @Tbl_Organisations t1
inner join @Tbl_Organisations t2 on t2.O_KeyID=@Random2
inner join @Tbl_Organisations t3 on t3.O_KeyID=@Random3
inner join @Tbl_Organisations t4 on t4.O_KeyID=@Random4
inner join @Tbl_Organisations t5 on t5.O_KeyID=@Random5
WHERE t1.O_KeyID=@Random1
--this shows the one row that was inserted
SELECT 'all unique',* FROM @Tbl_Campaigns
这是输出
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
@Lower=1, @Upper=3
@Lower=4, @Upper=6
@Lower=7, @Upper=9
@Lower=10, @Upper=12
@Lower=13, @Upper=15
(1 row(s) affected)
C_KeyID Cname createdate organisation_1 organisation_2 organisation_3 organisation_4 organisation_5
---------- ----------- ---------- ----------------------- -------------- -------------- -------------- -------------- --------------
all unique 1 YourName 2009-07-16 12:14:04.590 2 4 7 10 14
(1 row(s) affected)