| |

VerySource

 Forgot password?
 Register
Search
View: 704|Reply: 9

Dynamic SQL error during stored procedure

[Copy link]

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-2-6 00:30:01
| Show all posts |Read mode
.net environment prompt information:
"Line 1: Syntax error near '='."
The stored procedure is:


ALTER PROCEDURE Pr_UpdatePoint
(
@ManagerID int,
@departID int,
@StartTime datetime,
@EndTime datetime,
@NowTime datetime,
@TableName varchar (64)
)
AS
Ranch
declare @sqlcommand varchar (2000)
declare @ sqlcommand2 varchar (2000)
declare @ sqlcommand3 varchar (2000)
declare @ sqlcommand4 varchar (2000)
declare @tempID int
declare @TotalID int

select @ sqlcommand = 'select' + rtrim (@TotalID) + '= TotalID from' + @ TableName + 'where' + rtrim (@TotalID) + 'is null'
exec (@sqlcommand)
if @TotalID is null
begin
  select @ sqlcommand2 = 'insert into [TotalAssess]
(TotalPoint, StartTime, EndTime, NowTime)
SELECT SUM (RealPoint), '' '+ convert (varchar, @ StartTime, 120) +' '', '' '+ convert (varchar, @ EndTime, 120) +' '', '' '+ convert (varchar, @ NowTime, 120) + '' 'FROM' + @ TableName + 'where TotalID = null'
exec (@ sqlcommand2)


select @ sqlcommand3 = 'UPDATE Assess set TotalID =' + str (@TotalID) + 'where TotalID is null'
exec (@ sqlcommand3)

exec ('update TotalAssess set ManagerID =' + @ ManagerID + ', departID =' + @ departID + ', checkYN = "Unaudited" where TotalID =' + @ tempID + '')

end
 
Don't know what's wrong
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-3-25 08:30:01
| Show all posts
ALTER PROCEDURE Pr_UpdatePoint
(
@ManagerID int,
@departID int,
@StartTime datetime,
@EndTime datetime,
@NowTime datetime,
@TableName varchar (64)
)
AS
Ranch
declare @sqlcommand varchar (2000)
declare @ sqlcommand2 varchar (2000)
declare @ sqlcommand3 varchar (2000)
declare @ sqlcommand4 varchar (2000)
declare @tempID int
declare @TotalID int

select @ sqlcommand = 'select' + rtrim (@TotalID) + '= TotalID from' + @ TableName + 'where' + rtrim (@TotalID) + 'is null'
exec (@sqlcommand)
if @TotalID is null
begin
  select @ sqlcommand2 = 'insert into [TotalAssess] (TotalPoint, StartTime, EndTime, NowTime)
SELECT SUM (RealPoint), '' '+ convert (varchar, @ StartTime, 120) +' '', '' '+ convert (varchar, @ EndTime, 120) +' '', '' '+ convert (varchar, @ NowTime, 120) + '' 'FROM' + @ TableName + 'where TotalID is null'
  exec (@ sqlcommand2)


select @ sqlcommand3 = 'UPDATE Assess set TotalID =' + str (@TotalID) + 'where TotalID is null'
exec (@ sqlcommand3)

exec ('update TotalAssess set ManagerID =' + @ ManagerID + ', departID =' + @ departID + ', checkYN = `` Unaudited' 'where TotalID =' + @ tempID + '')

end

Look at it like this
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-3-25 10:15:02
| Show all posts
TotalID = null you are wrong
Should be TotalID is null
Reply

Use magic Report

0

Threads

49

Posts

35.00

Credits

Newbie

Rank: 1

Credits
35.00

 China

Post time: 2020-3-25 11:45:01
| Show all posts
Print the @sqlcommand variable
See what happens?
Reply

Use magic Report

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-4-1 09:15:01
| Show all posts
cv4bf5d65fd
---------------------------------------
I changed it, no, the two writing methods should be the same

michealhenry
----------------------------------------
I print the same result?
Reply

Use magic Report

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-5-4 01:45:01
| Show all posts
Let us help you to see if it is a syntax error or a program error?
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-5-4 21:00:01
| Show all posts
ALTER PROCEDURE Pr_UpdatePoint
(
@ManagerID int,
@departID int,
@StartTime datetime,
@EndTime datetime,
@NowTime datetime,
@TableName varchar (64)
)
AS

declare @sqlcommand varchar (2000)
declare @ sqlcommand2 varchar (2000)
declare @ sqlcommand3 varchar (2000)
declare @ sqlcommand4 varchar (2000)
declare @tempID int
declare @TotalID int --- add spaces here

select @ sqlcommand = 'select' + rtrim (@TotalID) + '= TotalID from' + @ TableName + 'where' + rtrim (@TotalID) + 'is null'
exec (@sqlcommand)
if @TotalID is null
begin
  select @ sqlcommand2 = 'insert into [TotalAssess] --- Add spaces here (TotalPoint, StartTime, EndTime, NowTime)
SELECT SUM (RealPoint), '' '+ convert (varchar, @ StartTime, 120) +' '', '' '+ convert (varchar, @ EndTime, 120) +' '', '' '+ convert (varchar, @ NowTime, 120) + '' 'FROM' + @ TableName + 'where TotalID is null'
  exec (@ sqlcommand2)


select @ sqlcommand3 = 'UPDATE Assess set TotalID =' + str (@TotalID) + 'where TotalID is null'
exec (@ sqlcommand3)

exec ('update TotalAssess set ManagerID =' + @ ManagerID + ', departID =' + @ departID + ', checkYN = `` not reviewed' 'where TotalID =' + @ tempID + '')

end

--Look at it like this. You have no spaces in two places
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-5-5 16:30:01
| Show all posts
exec ('update TotalAssess set ManagerID =' + @ ManagerID + ', departID =' + @ departID + ', checkYN = `` Unaudited' 'where TotalID =' + @ tempID + '')

This sentence is not correct, @ManagerID, @departID, @tempID are all int, can not be connected as a string, use rtrim () to expand,

exec ('update TotalAssess set ManagerID =' + rtrim (@ManagerID) + ', departID =' + rtrim (@departID) + ', checkYN = `` Unaudited' 'where TotalID =' + rtrim (@tempID))
Reply

Use magic Report

2

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-5-7 15:15:01
| Show all posts
cv4bf5d65fd:
Tip: There is a syntax error near 'rtrim'
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-5-22 04:00:01
| Show all posts
ALTER PROCEDURE Pr_UpdatePoint
(
@ManagerID int,
@departID int,
@StartTime datetime,
@EndTime datetime,
@NowTime datetime,
@TableName varchar (64)
)
AS

declare @sqlcommand varchar (2000)
declare @ sqlcommand2 varchar (2000)
declare @ sqlcommand3 varchar (2000)
declare @ sqlcommand4 varchar (2000)
declare @tempID int
declare @TotalID int

select @ sqlcommand = 'select' + @ TotalID + '= TotalID from' + @ TableName + 'where' + @ TotalID + 'is null'
exec (@sqlcommand)
if @TotalID = null
begin
select @ sqlcommand2 = 'insert into [TotalAssess] (TotalPoint, StartTime, EndTime, NowTime) SELECT SUM (RealPoint),' '' + convert (varchar, @ StartTime, 120) + '' ',' '' + convert (varchar , @ EndTime, 120) + '' ',' '' + convert (varchar, @ NowTime, 120) + '' 'FROM' + @ TableName + 'where TotalID is null'
  exec (@ sqlcommand2)

The
select @ sqlcommand3 = 'UPDATE Assess set TotalID =' + @ TotalID + 'where TotalID is null'
exec (@ sqlcommand3)

exec ('update TotalAssess set ManagerID =' + @ ManagerID + ', departID =' + @ departID + ', checkYN = `` Unaudited' 'where TotalID =' + @ tempID)

end
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