I've got a SQL query for inserting data into a table consisting of values for StorageRowNo
and StorageID
. The goal is to repeat this query a specified number of times with StorageRowNo
increasing by 1 every time the query is repeated. Any input would be greatly appreciated!
INSERT [dbo].[StorageRow]
SELECT StorageRowNo, StorageID
FROM (VALUES (1, 2)) V (StorageRowNo, StorageID)
WHERE NOT EXISTS (SELECT 1
FROM [dbo].[StorageRow] C
WHERE C.StorageRowNo = V.StorageRowNo
AND C.StorageID = V.StorageID);
Expected output would be something like this if the specified number were 3.
StorageRowNo | StorageID |
---|---|
1 | 2 |
2 | 2 |
3 | 2 |
use straight sql if you can
DECLARE @numRepeats INT = 10; -- number of times to repeat the query
INSERT [dbo].[StorageRow]
SELECT V.StorageRowNo, V.StorageID
FROM (select row_number() over(order by (select null)) as StorageRowNo
,2 as StorageID
from master..spt_values
) V
WHERE NOT EXISTS (SELECT 1
FROM [dbo].[StorageRow] C
WHERE C.StorageRowNo = V.StorageRowNo
AND C.StorageID = V.StorageID);
AND V.StorageRowNo<=@numRepeats
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.