简体   繁体   English

T-SQL:在 SELECT 中例程返回值时使用 t-sql

[英]T-SQL: Use t-sql while routine return value in SELECT

I have a T-SQL routine that copies user information from one table 'Radius' to another 'Tags'.我有一个 T-SQL 例程,可以将用户信息从一个表“Radius”复制到另一个“标签”。 However, as the rows are transfered, I would also like to include a unique randomly generated code in the INSERT (3 chars long).但是,随着行的传输,我还想在 INSERT 中包含一个唯一的随机生成的代码(3 个字符长)。 The code is generated by the WHILE loop below.代码由下面的 WHILE 循环生成。 Any way to do this?有什么办法可以做到这一点?

INSERT Tags (UserID, JobID, Code) 
SELECT UserID, @JobID,  ?????
FROM Radius

Unique random code generator:独特的随机码生成器:

WHILE EXISTS (SELECT * FROM Tags WHERE Code = @code)
BEGIN

select @code=@code+char(n) from
(
select top 3 number as n from master..spt_values 
where type='p' and number between 48 and 57 or number between 65 and 90
order by newid()
) 
END

CLARIFICATION: The reason for doing this is that I want to keep the random code generation logic at the level of the SQL stack.澄清:这样做的原因是我想将随机代码生成逻辑保持在 SQL 堆栈的级别。 Implementing this in the app code would require me to check the db everytime a potential random code is generated to see if it is unique.在应用程序代码中实现这一点需要我在每次生成潜在的随机代码时检查数据库以查看它是否是唯一的。 As the number of code records increases so will the number of calls to the db as probability increases that there will be more duplicate codes generated before a unique one is generated.随着代码记录数量的增加,对数据库的调用次数也会增加,因为在生成唯一代码之前将生成更多重复代码的概率增加。

Part One, Generate a table with all possible values第一部分,生成包含所有可能值的表

DECLARE @i int

CREATE TABLE #AllChars(value CHAR(1))

SET @i=48

WHILE @i<=57
BEGIN
    INSERT INTO #Allchars(value) VALUES(CHAR(@i))
    SET @i=@i+1
END

SET @i=65

WHILE @i<=90
BEGIN
    INSERT INTO #Allchars(value) VALUES(CHAR(@i))
    SET @i=@i+1
END

CREATE TABLE AllCodes(value CHAR(3),
          CONSTRAINT PK_AllChars PRIMARY KEY CLUSTERED(value))

INSERT INTO AllCodes(value)
SELECT AllChars1.Value+AllChars2.Value+AllChars3.Value
FROM #AllChars AS AllChars1,#AllChars AS AllChars2,#AllChars AS AllChars3

This is a one off operation and takes around 1 second to run on SQL Azure.这是一次性操作,在 SQL Azure 上运行大约需要 1 秒。 Now that you have all possible values in a table any future inserts become, something along the lines of现在您在表中拥有所有可能的值,任何未来的插入都会变成,类似于

SELECT
RadiusTable.UserID,
RadiusTable.JobID,
IDTable.Value
FROM 
(
  SELECT ROW_NUMBER() OVER (ORDER BY UserID,JobID) As RadiusRow,
  UserID,JobID
  FROM Radius
) AS RadiusTable INNER JOIN  

(
    SELECT ROW_NUMBER() OVER (ORDER BY newID()) As IDRow,
    Value
    FROM AllCodes
) AS IDTable ON RadiusTable.RadiusRow = IDTable.IDRow

Before going with any of these schemes you had better be certain that you are not going to have more than 46656 rows in your table otherwise you will run out of unique ID Values.在使用任何这些方案之前,您最好确定您的表中的行数不会超过 46656 行,否则您将用完唯一 ID 值。

I do not know if this is possible and suitable for your situation, but to me it seems that a scalar-valued function would be a solution.我不知道这是否可能并且适合您的情况,但对我来说,标量值 function 似乎是一个解决方案。

Well, let me start over then.好吧,让我从头开始吧。

This seems kind of ugly but it might work: newid() inside sql server function这看起来有点难看,但它可能会起作用: newid() inside sql server function

The accepted answer that is.公认的答案是。

Ah, been there done that too.啊,那里也做过。 The problem with this is that I am using T-SQL Stored Procedures that are called by Asp.net Where would I put the CREATE VIEW statement?问题在于我正在使用由 Asp.net 调用的 T-SQL 存储过程,我将 CREATE VIEW 语句放在哪里? I can't add it to the function file.我无法将其添加到 function 文件中。

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

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