| |

VerySource

 Forgot password?
 Register
Search
View: 1207|Reply: 12

Table variable, IDENTITY_INSERT, order by problem?

[Copy link]

3

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-2 22:10:01
| Show all posts |Read mode
declare @tt table (sid tinyint identity (1,1), v decimal (18,2), st tinyint)

insert into @tt
select # 1.vs,
case
when ... then 0
when ... then 1
when ... then 2
end si
from fd () # 1
inner join
fs ('') # 3
on # 1.id = # 3.id
order by vs, si

It will go wrong:
Server: Msg 8101, Level 16, State 1, Line 3
You can specify an explicit value for the identity column in the table '@tt' only if a list of columns is used and IDENTITY_INSERT is ON.

If you remove order by vs, si, nothing will go wrong!

vs is decimal data.
Reply

Use magic Report

0

Threads

7

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-2 22:51:01
| Show all posts
Does the auto-increment column insert values? You can remove the auto-increment
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-2 22:57:01
| Show all posts
With table variables, looks like the problem of # 1 temporary table. Change the other table names without using ##
If the insert table has
set IDENTITY_INSERT on-need to be defined to insert
set IDENTITY_INSERT off
Reply

Use magic Report

3

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-1-2 23:00:01
| Show all posts
Must be automatically incremented.
Why removing order by is not wrong, but it is not the result I want.
Reply

Use magic Report

3

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-1-2 23:03:01
| Show all posts
I used the above code in a custom function.
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 United States

Post time: 2020-1-2 23:06:01
| Show all posts
insert into @tt
select # 1.vs,
case
when ... then 0
when ... then 1
when ... then 2
end si
from fd () # 1
inner join
fs ('') # 3
on # 1.id = # 3.id
order by vs,
case when ... then 0 when ... then 1 when ... then 2 end-so that nothing goes wrong
Reply

Use magic Report

3

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-1-2 23:18:01
| Show all posts
why? Still wrong
Server: Msg 8101, Level 16, State 1, Line 3
You can specify an explicit value for the identity column in the table '@tt' only if a list of columns is used and IDENTITY_INSERT is ON.
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-2 23:30:01
| Show all posts
set IDENTITY_INSERT on-need to be defined to insert
--Insert data
set IDENTITY_INSERT off
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-2 23:33:01
| Show all posts
set IDENTITY_INSERT off--1
Inserted statement
set IDENTITY_INSERT on-turn on unique ID
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-2 23:39:01
| Show all posts
The syntax is:
SET IDENTITY_INSERT table name off


SET IDENTITY_INSERT table name ON
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list