• wendy elizabeth (10/11/2016)


    In a t-sql 2012, I want to place the new lockid values obtain from the first insert statement (for the lock table) indiviudally into the second insert statement (locker table in the in the column called lockid).

    ....

    If your intention is to copy the locker data set (locker, lock & lockcombination tables) for SchoolID=12 and create a new locker data set from it, with new LockID's but the same data, for SchoolID = 134, then you will end up with incorrect data for SchoolID = 134. All the serial numbers of the locks and lockers (and their combinations) will be for SchoolID=12. You will have rubbish data.

    If on the other hand the locker data set with serial numbers and everything for SchoolID=12 is actually for SchoolID = 134, then simply change the SchoolID from 12 to 134 in the locker data set.

    Please clarify what you are trying to do, and why you want to do it. Refusing to comply with this simple request has already cost you a week.

    As an aside, always use a column list for inserts. They are mandatory in certain situations. They are infinitely useful to people attempting to help you on ssc.

    INSERT INTO [dbo].[Lock] (column list here)

    SELECT 134, k.serialNumber, k.type,2

    FROM [dbo].[Lock] k

    INNER JOIN [dbo].[Locker] l

    ON l.lockID = k.lockID

    AND l.locationID BETWEEN 1552 and 1555

    ORDER BY l.locationID, l.number

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden