简体   繁体   English

如何在 SQL Server 中将数字转换为 nvarchar?

[英]How to convert numeric to nvarchar in SQL Server?

Consider the following table.考虑下表。 I use a trigger to add to the table.我使用触发器添加到表中。 In the column of converting the number to the string, it fails.在将数字转换为字符串的列中,它失败了。

CREATE TABLE [dbo].[tblAIAgent]
(
    [AgentCode] [NVARCHAR](10) NOT NULL,
    [NationalCode] [BIGINT] NOT NULL 
         CONSTRAINT [DF_tblAIAgent_NationalCode]  DEFAULT ((0)),
    [FirstName] [NVARCHAR](50) NOT NULL 
         CONSTRAINT [DF_tblAIAgent_Name] DEFAULT (''),
    [LastName] [NVARCHAR](50) NOT NULL 
         CONSTRAINT [DF_tblAIAgent_Family] DEFAULT (''),
    [IsActive] [BIT] NOT NULL 
         CONSTRAINT [DF_tblAIAgent_Active] DEFAULT ((1)),
    [Counter] [INT] IDENTITY(1,1) NOT NULL,

    CONSTRAINT [PK_tblAIAgent] 
        PRIMARY KEY CLUSTERED 
)

ALTER TRIGGER [dbo].[AgentInsert] 
ON [dbo].[tblAIAgent]
INSTEAD OF INSERT
AS
BEGIN
    DECLARE @AgentCode NVARCHAR(10)
    DECLARE @NationalCode BIGINT
    DECLARE @FirstName NVARCHAR(50)
    DECLARE @LastName NVARCHAR(50)
    DECLARE @IsActive BIT
    DECLARE @CounterIs INT

    SET @CounterIs = @@IDENTITY

    SELECT
        @AgentCode = AgentCode,
        @NationalCode = NationalCode,
        @FirstName = FirstName,
        @LastName = LastName,
        @IsActive = IsActive 
    FROM inserted

    INSERT INTO tblAIAgent (NationalCode, FirstName, LastName, IsActive, AgentCode)
    VALUES (@NationalCode, @FirstName, @LastName, @IsActive, 'Agent_' + CAST(@CounterIs AS NVARCHAR(4))) 
END

You have a few problems here:你在这里有几个问题:

The @@IDENTITY is a system function contains the last identity value that is generated when an INSERT , SELECT INTO , or BULK COPY statement is completed. @@IDENTITY是一个系统函数,包含在INSERTSELECT INTOBULK COPY语句完成时生成的最后一个标识值。 If the statement did not affect any tables with identity columns, @@IDENTITY returns NULL .如果该语句不影响具有标识列的任何表, @@IDENTITY返回NULL If multiple rows are inserted, generating multiple identity values, @@IDENTITY returns the last identity value generated.如果插入多行,生成多个标识值, @@IDENTITY返回生成的最后一个标识值。

In your case, you have an INSTEAD OF INSERT trigger, so there is no INSERT .在您的情况下,您有一个INSTEAD OF INSERT触发器,因此没有INSERT

This below query is completely wrong and will gives wrong results, it works as expected only if one row inserted, if there is more than 1 row, then those variables will hold just the values of one row, and you will lose the other values of the other rows, cause the pseudo INSERTED may contains 1 or more rows下面的查询是完全错误的,会给出错误的结果,只有在插入一行时它才能按预期工作,如果有超过 1 行,那么这些变量将只保存一行的值,而您将丢失其他值其他行,导致伪INSERTED可能包含 1 行或更多行

select @AgentCode=AgentCode,
       @NationalCode=NationalCode,
       @FirstName=FirstName,
       @LastName=LastName,
       @IsActive=IsActive 
from inserted

Now, looking to your table, you already have an IDENTITY column, so you don't need to a TRIGGER at all, you can just make a computed column as现在,查看您的表,您已经有一个IDENTITY列,因此您根本不需要TRIGGER ,您只需创建一个计算列即可

CREATE TABLE [dbo].[tblAIAgent](
    [AgentCode] AS 'Agent_' + CAST([Counter] AS VARCHAR(10)),
    [NationalCode] [bigint] NOT NULL 
    CONSTRAINT [DF_tblAIAgent_NationalCode]  DEFAULT ((0)),
    [FirstName] [nvarchar](50) NOT NULL 
    CONSTRAINT [DF_tblAIAgent_Name]  DEFAULT (''),
    [LastName] [nvarchar](50) NOT NULL 
    CONSTRAINT [DF_tblAIAgent_Family]  DEFAULT (''),
    [IsActive] [bit] NOT NULL 
    CONSTRAINT [DF_tblAIAgent_Active]  DEFAULT ((1)),
    [Counter] [int] IDENTITY(1,1) NOT NULL,
    CONSTRAINT [PK_tblAIAgent] PRIMARY KEY ([Counter])
    );

UPDATE:更新:

According to your comment "a computed column can no longer be selected as the PK. I want this column to be placed in other relevant tables as a FK.I wrote the trigger to get the column value instead of the computed column so that I can select the column as the primary key" .根据您的评论“无法再选择计算列作为 PK。我希望将此列作为 FK 放置在其他相关表中。我编写了触发器来获取列值而不是计算列,以便我可以选择该列作为主键" You are trying to make it a PRIMARY KEY so you can do as您正在尝试将其设为PRIMARY KEY以便您可以这样做

CREATE TABLE T(
  Counter INT IDENTITY(1,1) NOT NULL,
  OtherCol INT,
  Computed AS CONCAT('Agent_', CAST(Counter AS VARCHAR(10))) PERSISTED,
  CONSTRAINT PKT PRIMARY KEY(Computed)
);

CREATE TABLE TT(
  ReferenceComputedColumn VARCHAR(16) NOT NULL,
  OtherColumn INT,
  CONSTRAINT FK_ReferencedComputedColumn 
      FOREIGN KEY(ReferenceComputedColumn) 
      REFERENCES T(Computed)
)

INSERT INTO T(OtherCol) VALUES
(1), (2), (3);

INSERT INTO TT(ReferenceComputedColumn, OtherColumn) VALUES
('Agent_1', 10),
('Agent_3', 20);

SELECT *
FROM T LEFT JOIN TT 
ON T.Computed = TT.ReferenceComputedColumn;

See how it's working . 看看它是如何工作的

See also this article Properly Persisted Computed Columns by Paul White .另请参阅Paul White撰写的这篇文章Properly Persisted Computed Columns

尝试这个

SELECT CONVERT(NVARCHAR(255), @AgentCode)
SELECT CAST([PictureId] AS NVARCHAR(4)) From Category

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM