troubleshooting Question

Query syntax problem

Avatar of DancingFighterG
DancingFighterG asked on
Microsoft SQL Server
6 Comments1 Solution217 ViewsLast Modified:
Hello, i'm trying to write this sql command with text and I'm getting an error:

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'a'.

Here is my code:

select @sql = 'select "' + @@servername + '" as "ServerName", convert(char(32), "' + @db_name + '") as "DatabaseName", journal_type, journal_ctrl_num, journal_description, date_entered, date_applied, recurring_flag, repeating_flag, reversing_flag, hold_flag, posted_flag, date_posted, source_batch_code, batch_code, type_flag, intercompany_flag, company_code, app_id, home_cur_code, document_1, trx_type, user_id, source_company_code, process_group_num, oper_cur_code '
      select @sql = @sql + 'from [' + @db_name + ']..gltrx a'
      select @sql = @sql + 'where a.date_entered between "' + convert(varchar, @from_date) + '" and "' + convert(varchar, @to_date) + '" '
      select @sql = @sql + 'and gltrx not in ("__NDES__", "afe", "BNA_Appalachia", "BNA_Basin", "BNA_Hydrocarbon", "BNA_JavelinaCompany", "BNA_JavelinaPipeline", "BNA_Michigan", "BNA_MichiganPipeline", "BNA_MichiganPipeline-FERC", "BNA_MLP", "BNA_Westshore", "CitrixIMA", "coffice", "core", "Epicor_Analytics", "ExpressOptions_MWE", "ExpressOptions_MWP", "master", "MAXIMO", "MAXTEST", "model", "msdb", "MWIntranet", "pipelines", "PLTMASTER", "pricing", "producer", "production", "RAM", "RiskAssetManager", "sample", "SQLAudit", "stellent", "standard", "temp_PLTMASTER", "TempChartConvert", "tempdb", "transfer", "workflow"'
      select @sql = @sql + 'order by date_entered DESC'
      Print '********************************************************************************************************************************'
      Print @db_name
      exec (@sql)

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros