Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identity column in TVP #760

Open
scruper opened this issue Jul 12, 2022 · 1 comment
Open

Identity column in TVP #760

scruper opened this issue Jul 12, 2022 · 1 comment

Comments

@scruper
Copy link

scruper commented Jul 12, 2022

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

@NikitaDef
Copy link
Contributor

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

Hi, check #771 my MR.
How to use.

For field set tag at tvp:"@identity".

Loot at the example in test.
https://github.com/denisenkom/go-mssqldb/pull/771/files#diff-190109b133bdf8751fdc477fd4431d277075488688e403feeb86b0e1858c4dbbR1352

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants