| |

VerySource

 Forgot password?
 Register
Search
View: 1275|Reply: 7

What should I do if the temporary table created in dynamic SQL cannot be accessed?

[Copy link]

2

Threads

18

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-2-2 15:40:02
| Show all posts |Read mode
SELECT @ N = MAX (LEN (F0080)) FROM KLDB.DBO.OTH_D_NEWS

--Temporary table, the maximum ID is the maximum length of the relevant stock field
SET @ SQL = 'SELECT TOP' + CONVERT (VARCHAR (10), @ N) + 'ID = IDENTITY (INT, 1,1)
INTO # FROM SYSCOLUMNS A, SYSCOLUMNS B '

EXECUTE SP_EXECUTESQL @SQL

SELECT * FROM #
---------------------------------------
Server: Message 208, Level 16, State 1, Process P_GENERATE_TB8002, Line 20
Object name '#' is invalid.

What should I do?
Reply

Use magic Report

0

Threads

66

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-3-21 08:15:01
| Show all posts
Do not build temporary tables, manually drop after use
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Unknown

Post time: 2020-3-21 10:00:02
| Show all posts
@SQL
Defined as nvarchar
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 Japan

Post time: 2020-3-21 10:30:01
| Show all posts
Try with global temporary tables
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-21 11:15:02
| Show all posts
#tableName ----> ## tableName
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-3-21 12:15:02
| Show all posts
SELECT @ N = MAX (LEN (F0080)) FROM KLDB.DBO.OTH_D_NEWS

--Temporary table, the maximum ID is the maximum length of the relevant stock field
SET @ SQL = 'SELECT TOP' + CONVERT (VARCHAR (10), @ N) + 'ID = IDENTITY (INT, 1,1)
INTO #a FROM SYSCOLUMNS A, SYSCOLUMNS B '

EXECUTE SP_EXECUTESQL @SQL

SELECT * FROM #a


Temporary table also needs table name--#
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-3-21 13:15:01
| Show all posts
--try

SELECT @ N = MAX (LEN (F0080)) FROM KLDB.DBO.OTH_D_NEWS

--Temporary table, the maximum ID is the maximum length of the relevant stock field
SET @ SQL = 'SELECT TOP' + CONVERT (VARCHAR (10), @ N) + 'ID = IDENTITY (INT, 1,1)
INTO ## T FROM SYSCOLUMNS A, SYSCOLUMNS B '

EXECUTE SP_EXECUTESQL @SQL

SELECT * FROM ## T
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-3-21 16:45:02
| Show all posts
The landlord's usage is incorrect, take a look at the online help is more useful to you
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