简体   繁体   中英

Table valued Function - Identity(seed, increment)

I have a Table Valued Function qlikview_verlauf . I want to return a table with id , Date and Path .

Identity(seed, increment)

The ID is an autoincrement value. I want to start this autoincrement (seed) from the max(id)+1 from another table named Testfortschritt .

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.

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. If you have a specific order just replace (SELECT 1) with your order columns.

Since the ID should be uniqe I also omitted the PARTITION BY clause which isn't needed in your scenario.

More about ROW_NUMBER can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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