Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to Dynamic SQL need help with syntax error

Status
Not open for further replies.

avid1741

Technical User
May 30, 2002
10
US
I keep getting the error:Server: Msg 156, Level 15, State 1, Line 26
Incorrect syntax near the keyword 'From'.
When I try to run the following code.
Any help in solving this is greatly appreciated.
I assume the error is referring to the last line where the FROM exist.

Declare @InPutDate as VarChar(255)
SET @InPutDate = '_'+ CAST(Datepart(month,GetDate()) as VarChar(2)) + '_W'
+ CAST(Datepart(Month,GetDate())as VarChar(2)) + CAST(Datepart(Day,GetDate()) as Varchar(2))+
'_' + CAST(Right(Datepart(Year,Getdate()),2)as VarChar(2))+ '_Test'

Declare @OutPutDate as VarChar(255)
SET @OutPutDate = '_'+ CAST(Datepart(month,GetDate()) as VarChar(2)) + '_W'
+ CAST(Datepart(Month,GetDate())as VarChar(2)) + CAST(Datepart(Day,GetDate()) as Varchar(2))+
'_' + CAST(Right(Datepart(Year,Getdate()),2)as VarChar(2))+ '_Test'

---Source Data tables Distribution Names
Declare @DistNamesSrcTable as VarChar(255)
Set @DistNamesSrcTable = 'Distribution_Names'+ @InPutDate

-----Output Table
Declare @AgentTble as VarChar (255)
Set @AgentTble = 'Distribution_Names'+ @OutPutDate
Declare @StrSQL as VarChar(5000)

--Here @AgentTble = Distribution_Names
--Declare @StrSQL as VarChar(5000)
--Set @StrSql = N'Select distinct number as agent_code INTO '+ @AgentTble + ' From ' + @AgentSrcTable +
--' Create index ix_1 on '+ @AgentTble + '(Company_id,contract,account)'

Set @StrSql = N'Delete * + ' From '+ @DistNamesSrcTable'
+ 'number = '8' and code ='400''
 
Set @StrSql = N'Delete * + ' From '+ @DistNamesSrcTable'
+ 'number = '8' and code ='400''

You need a 'WHERE' in there after the table name.
 
Set @StrSql = N'Delete * + ' From '+ @DistNamesSrcTable'
+ 'number = '8' and code ='400''
This line is incorrect
-You don't put a * in a delete statement
-the where keyword is missing
So the correct syntax is:
Set @StrSql = N'Delete ' From '+ @DistNamesSrcTable'
+ 'WHERE number = '8' and code ='400''

Enjoy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top