简体   繁体   English

表值函数-身份(种子,增量)

[英]Table valued Function - Identity(seed, increment)

I have a Table Valued Function qlikview_verlauf . 我有一个表值函数qlikview_verlauf I want to return a table with id , Date and Path . 我想返回一个id DatePath的表。

Identity(seed, increment)

The ID is an autoincrement value. ID是一个自动增量值。 I want to start this autoincrement (seed) from the max(id)+1 from another table named Testfortschritt . 我想从另一个名为Testfortschritt表的max(id)+1开始此自动增量(种子)。

I have tried the following, but it doesn't work. 我已经尝试了以下方法,但是没有用。 The Error Message is incorrect Syntax. 错误消息是不正确的语法。

Create FUNCTION [dbo].[qlikview_verlauf](@param INT)
    RETURNS @qlikview_verlauf table (
           ID INT IDENTITY((Select max(id) from Testfortschritt),1) 
          ,[Date] date NOT NULL
          ,[Path] varchar(600) NOT NULL
    )

I would set aside the IDENTITY of your ID column and rather use ROW_NUMBER to generate the ID in your SELECT statement. 我会留出ID列的IDENTITY ,而是使用ROW_NUMBERSELECT语句中生成ID。

For example: 例如:

SELECT
   (SELECT MAX(id) FROM Testfortschritt) +
      ROW_NUMBER OVER(ORDER BY (SELECT 1)) AS ID,
   [Date],
   [Path]
FROM <YourTable>

Since I don't know how your exact statement looks like, I used ORDER BY (SELECT 1) which lets SQL Server decide in which order records are numbered. 由于我不知道您的确切语句的样子,因此我使用了ORDER BY (SELECT 1) ,它使SQL Server可以确定记录的编号顺序。 If you have a specific order just replace (SELECT 1) with your order columns. 如果您有特定的订单,只需将(SELECT 1)替换为您的订单列。

Since the ID should be uniqe I also omitted the PARTITION BY clause which isn't needed in your scenario. 由于ID应该是唯一的,所以我也省略了您的方案中不需要的PARTITION BY子句。

More about ROW_NUMBER can be found here 有关ROW_NUMBER更多信息,请参见此处

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

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