I am trying to transfer data from a column from one table to another, both columns are with unique identifiers and when i transfer the data it is copying the column after the end of the data of the second table. After the end of the other column of the second table (I am inserting first random integers in the first column and then I want to copy the information from another table in the same database and it is starting after the 135th row (I add 135 rows with random ints)). First table name : carBrand and column name model_id - second table name Cars11, model_idss - or whatever is the name... THE QUESTION IS WHY is it inputting the iformation after the first input - example - i am inputting 135 random ints and after that i am trying to copy the information from the other table and when i am pasting it, the information is pasted after the 136th to the 270th sign My query is looking like this for the new table
DECLARE @Min_Value AS int
SET @Min_Value= 15000
DECLARE @Max_Value AS int
SET @Max_Value = 1000000
DECLARE @n AS int
SET @n = 135
BEGIN TRANSACTION
DECLARE @uid uniqueidentifier
SET @uid = NEWID()
DECLARE @i AS int
SET @i = 1
WHILE @i <= @n BEGIN INSERT INTO Cars11([Model_prices]) VALUES(FLOOR(RAND(CHECKSUM(@uid))*(@Max_Value - @Min_Value +1) + @Min_Value)) SET @i += 1 SET @uid = NEWID() END COMMIT TRANSACTION
INSERT INTO Cars11(model_idss)
SELECT model_id
FROM carBrand
WHERE model_id <= 135;
It would be easier to parse your query if you used the code sample block (ctrl-k)
You need to do an update instead of insert for the second insert into Cars11.
Update changes already existing records, Insert creates new records.
Something like this:
DECLARE @Min_Value AS int
SET
@Min_Value = 15000
DECLARE @Max_Value AS int
SET
@Max_Value = 1000000
DECLARE @n AS int
SET
@n = 135
BEGIN TRANSACTION
DECLARE @uid uniqueidentifier
SET
@uid = NEWID()
DECLARE @i AS int
SET
@i = 1 WHILE @i <= @n
BEGIN
INSERT INTO
Cars11([Model_prices])
VALUES
(
FLOOR(
RAND(CHECKSUM(@uid)) *(@Max_Value - @Min_Value + 1) + @Min_Value
)
)
SET
@i + = 1
SET
@uid = NEWID()
END
COMMIT TRANSACTION
UPDATE Cars11
Set model_idss = (Select model_id FROM carBrand WHERE Cars11.model_idss = carBrand.model_id and carBrand.model_id <= 135));
Here are some other options for updating a column based on a query result
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.