久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL Server 獲取標識號并分配給另一個列值

SQL Server get Identity Number and assign to another column value(SQL Server 獲取標識號并分配給另一個列值)
本文介紹了SQL Server 獲取標識號并分配給另一個列值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是 SQL Server 初學者,需要滿足要求但不知道如何

I am a beginner SQL Server student and need to fulfill a requirement and don't know how

對于表 Products 我有列 ProductIDProductNameSerialNo.

For table Products I have columns ProductID, ProductName, SerialNo.

SerialNo 應該從 1001 開始并加 1,同時 ProductId 應該從 P1001, P1002<開始/code> 等等...

The SerialNo should start from 1001 and increment by 1 and at the same time ProductId should start as P1001, P1002 and so...

我定義了

SerialNo INT Identity(1001, 1)

并且不知道如何獲取標識值并將其附加到P"并嘗試使用變量但無法弄清楚

and don't know how to take the identity value and append it to 'P' and tried using variables and can't figure it out

BEGIN 
    DECLARE @ProductID VARCHAR(5)
    SET @ProductID = 'P' + CAST(@@IDENTITY AS VARCHAR)
    INSERT INTO Product VALUES(@ProductID,'Nokia')
    SELECT * FROM Product
END

我得到了

ProductID       Name        SerialNo
--------------------------------------
NULL            NOKIA       1001

預期輸出是

ProductID       Name        SerialNo
-------------------------------------
P1001            NOKIA       1001

推薦答案

@@IDENTITY 保留會話中最后一個插入標識,所以它對你沒有用,你有幾個選擇:

@@IDENTITY keeps the last insert identity in the session, so its not useful for you , you have several option:

  • 添加計算列:

  • add a computed column :

alter table product add ProductId as concat('P',SerialNo)  

  • 使用 IDENT_CURRENT :IDENT_CURRENT 為您提供表中的最后一個標識值

  • use IDENT_CURRENT : IDENT_CURRENT give you the last identity values in the table

    INSERT INTO Product VALUES(concat('P',IDENT_CURRENT('dbo.product')+1) ,'Nokia')
    SELECT * FROM Product
    

  • 我建議你使用計算列,但是你總是可以重現 productId ,不知道為什么你需要保存它,它是冗余的

    I recommend you go with computed column , however you always can reproduce the productId , not sure why you need to save it , its redundunt

    這篇關于SQL Server 獲取標識號并分配給另一個列值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

    相關文檔推薦

    Modify Existing decimal places info(修改現有小數位信息)
    The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
    T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
    remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
    Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
    concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
    主站蜘蛛池模板: 欧美精品二区三区四区免费看视频 | 欧美一区二区在线观看 | 一级看片免费视频 | 久久亚洲精品视频 | 狠狠干夜夜| 特级特黄aaaa免费看 | 国产农村妇女aaaaa视频 | 精品免费视频 | 一级黄色性生活片 | 亚洲免费黄色 | 免费的毛片 | 青青草免费在线观看视频 | 久草网站 | 日本黄色免费看 | 中文字幕在线免费观看 | 黄色大片视频 | 国产高清视频在线观看 | 欧美又大粗又爽又黄大片视频 | 欧美日韩一区二区三区四区 | 日韩精品一区在线观看 | 80日本xxxxxxxxx96 国产成人在线免费视频 | 一级片av | 成 人 黄 色 片 在线播放 | 日本一区二区不卡视频 | 高清一级片 | 久久精品观看 | 特黄aaaaaaaaa真人毛片 | 亚洲综合视频在线观看 | 国产精品久久久久久久久久久久午夜片 | 国产激情一区二区三区 | 三级视频在线 | 日韩中文字幕第一页 | www.久久久 | 久操视频在线 | 亚洲视频国产 | 夜夜嗨av一区二区三区 | 国产伦精品一区二区三区免费视频 | 中文字幕www | 亚洲成人二区 | 91调教视频 | 91精品国产99久久久久久红楼 |