| |

VerySource

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

Can this be written in a stored procedure? Why does it always go wrong?

[Copy link]

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-28 13:20:02
| Show all posts |Read mode
declare @sqlcommand char (200)

select @ sqlcommand = 'select' + rtrim (@TableName) + '. AssessID,' + rtrim (@TableName) + '. RealPoint,' + rtrim (@TableName) + '. assessAccord,' + rtrim (@TableName) + '.ItemComment, AssessItem.ItemName, manager.ManagerName, TotalAssess.CheckComment, TotalAssess.StartTime, TotalAssess.EndTime, TotalAssess.NowTime
        from manager, TotalAssess, AssessItem, '+ rtrim (@TableName) +' where TotalAssess.TotalID = '+ rtrim (@TotalID) +' and TotalAssess.TotalID = Assess.TotalID and Assess.ManagerID = '+ rtrim (@ManagerID) + 'and Assess.ManagerID = manager.ManagerID and Assess.ItemID = AssessItem.ItemID'

  EXEC (@sqlcommand)

.net error message:
The column prefix 'xlglyike' does not match the table name or alias used in the query.
The column prefix 'xlglyike' does not match the table name or alias used in the query.
The column prefix 'xlglyike' does not match the table name or alias used in the query.
The column prefix 'xlglyike' does not match the table name or alias used in the query.
The column prefix 'AssessItem' does not match the table name or alias used in the query.
The column prefix 'manager' does not match the table name or alias used in the query.
The column prefix 'TotalAssess' does not match the table name or alias used in the query.
The column prefix 'TotalAssess' does not match the table name or alias used in the query.
The column prefix 'TotalAssess' does not match the table name or alias used in the query.

'xlglyike' is the dynamic table name
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-2-24 10:30:01
| Show all posts
What is @TableName
Reply

Use magic Report

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-2-24 11:00:01
| Show all posts
ALTER PROCEDURE Pr_GetMorePoint
(
    @TotalID int,
    @ManagerID int,
    @TableName varchar (64)
)
AS
This is the @TableName above is the dynamic table name
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-2-24 13:45:02
| Show all posts
declare @sqlcommand char (200)
Change this sentence to
declare @sqlcommand varchar (8000)
Reply

Use magic Report

0

Threads

93

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-2-24 14:30:01
| Show all posts
The variable is not processed well
declare @sqlcommand varchar (200), @tablename varchar (100), @TotalID int, @ManagerID int
set @ tablename = 'ta'
select @ sqlcommand = 'select' + rtrim (@TableName) + '. AssessID,' + rtrim (@TableName) + '. RealPoint,' + rtrim (@TableName) + '. assessAccord,' + rtrim (@TableName) + '.ItemComment, AssessItem.ItemName, manager.ManagerName, TotalAssess.CheckComment, TotalAssess.StartTime, TotalAssess.EndTime, TotalAssess.NowTime
        from manager, TotalAssess, AssessItem, '+ rtrim (@TableName) +' where TotalAssess.TotalID = '+ rtrim (@TotalID) +' and TotalAssess.TotalID = Assess.TotalID and Assess.ManagerID = '+ rtrim (@ManagerID) + 'and Assess.ManagerID = manager.ManagerID and Assess.ItemID = AssessItem.ItemID'

  EXEC (@sqlcommand) This will cause an error
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-2-24 16:30:02
| Show all posts
declare @sqlcommand varchar (8000)

select @ sqlcommand = 'select' + rtrim (@TableName) + '. AssessID,' + rtrim (@TableName) + '. RealPoint,' + rtrim (@TableName) + '. assessAccord,' + rtrim (@TableName) + '.ItemComment, AssessItem.ItemName, manager.ManagerName, TotalAssess.CheckComment, TotalAssess.StartTime, TotalAssess.EndTime, TotalAssess.NowTime from manager, TotalAssess, AssessItem,' + rtrim (@TableName) + 'where TotalAssess.TotalID =' + str (@TotalID) + 'and TotalAssess.TotalID = Assess.TotalID and Assess.ManagerID =' + str (@ManagerID) + 'and Assess.ManagerID = manager.ManagerID and Assess.ItemID = AssessItem.ItemID'

EXEC (@sqlcommand)
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-2-29 22:15:01
| Show all posts
@TotalID int,
@ManagerID int,

These two variables are integers. You need to use str () function to convert them into strings to concatenate. Also, int types cannot use rtrim ~~~
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-3-2 20:30:02
| Show all posts
declare @sqlcommand char (200)
The first question should be here

The variable is too short, just before the table name
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