Skip to content

Identity column in TVP #760

Open
Open
@scruper

Description

@scruper

Hi, is it possible to insert into TVP which has identity field.

Now I get "INSERT into an identity column not allowed on table variables"

Code example

db, _ := createDbConn() defer db.Close()

createTVP := `
CREATE TYPE dbo.TestType AS TABLE
(
	id INT IDENTITY(1,1) PRIMARY KEY,
	counter INT
);`
_, err := db.Exec(createTVP)

createProc := `
CREATE PROCEDURE dbo.TestProc
	@TVP dbo.TestType READONLY
AS
	SELECT * from @TVP
`
_, err = db.Exec(createProc)

type TestType struct {
	Counter int
}

TestTableType := []TestType{
	{
		Counter: 1,
	},
	{
		Counter: 3,
	},
}

tvpType := mssql.TVP{
	TypeName: "TestType",
	Value:    TestTableType,
}

_, err = db.Exec("exec dbo.TestProc @TVP;", sql.Named("TVP", tvpType))

if err != nil {
	fmt.Println(err)
}

Error: mssql: Trying to pass a table-valued parameter with 1 column(s) where the corresponding user-defined table type requires 2 column(s).

Using SQL Server Directly:
`
declare @tvp dbo.TestType

insert @tvp ([counter])
values (1),(2)

exec dbo.TestProc @tvp
`
Works fine

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions