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

SQL Server 獲取標(biāo)識號并分配給另一個列值

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

問題描述

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

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 應(yīng)該從 1001 開始并加 1,同時 ProductId 應(yīng)該從 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)

并且不知道如何獲取標(biāo)識值并將其附加到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

預(yù)期輸出是

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

推薦答案

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

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

  • 添加計(jì)算列:

  • add a computed column :

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

  • 使用 IDENT_CURRENT :IDENT_CURRENT 為您提供表中的最后一個標(biāo)識值

  • 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
    

  • 我建議你使用計(jì)算列,但是你總是可以重現(xiàn) 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

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

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

    相關(guān)文檔推薦

    Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
    The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
    T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
    remove duplicates from comma or pipeline operator string(從逗號或管道運(yùn)算符字符串中刪除重復(fù)項(xiàng))
    Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
    concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
    主站蜘蛛池模板: 黄色在线观看网址 | 国产东北一级毛片 | 国产精品视频网 | 一区二区三区中文字幕 | 99在线播放 | 91精品国产一区二区三区 | 欧美日韩国产一区二区三区 | 久久久久久久综合色一本 | 国产精品色 | 蜜桃精品视频在线 | 国产美女黄色片 | 欧美一页 | 精品国产一区二区在线 | 国产精品国产自产拍高清 | 日韩精品极品视频在线观看免费 | 激情小说综合网 | 99国产精品久久久久久久 | 国产精品1区2区3区 欧美 中文字幕 | av网站免费 | 一区在线观看视频 | 在线观看视频h | 日本天堂一区二区 | 精品久久久久久久久久久久久久 | 国产精品一区二 | 日本精品裸体写真集在线观看 | 美女久久久 | 国产成人精品免费视频大全最热 | 国产专区在线 | 国产高清在线观看 | 国产成人高清视频 | 国产精品久久久久久高潮 | 伊人色综合久久天天五月婷 | 蜜桃av一区二区三区 | 亚洲欧美日韩在线不卡 | 日韩免费一区二区 | 国产精品区二区三区日本 | 久久精品免费 | 欧美日韩淫片 | 在线视频一区二区 | 国产精品日女人 | 国产亚洲网站 |