Solved

Dynamic SQL

Posted on 2011-03-19
2
282 Views
Last Modified: 2012-05-11
I am having trouble with dynamic sql statements.  Here is what I need:

I will have a table variable, a variable for machine name and a variable for manufacturing order (@MO).  If the value in the table variable is ‘PREMIER’, the table name is PREMIER_SO_MO and the MO is MO-12345, I want the statement to say:

'SELECT * INTO PREMIER_SO_MO FROM PCT_SNUGZ_FILTER WHERE MACHINE = ‘PREMIER’ AND LEFT([WORK ORDER NUMBER], 9) = 'MO-12345’

If I test by using this code:

DECLARE @MACHINE VARCHAR(31), @TBLNAME1 VARCHAR(50), @MACHINE VARCHAR(50), @COMMAND VARCHAR(1000)

SELECT @MACHINE = ‘PREMIER’
SELECT @TBLNAME1 = ‘PREMIER_SO_MO’
SELECT @MO = ‘MO-12345’

SELECT @COMMAND = 'SELECT * INTO ' + @TBLNAME1 + ' FROM PCT_SNUGZ_FILTER WHERE MACHINE = ''' + @MACHINE1 + ''' AND LEFT([WORK ORDER NUMBER], 9) = ''' + @MO + ''''
      print @machine
      print @tblname1
      print @command

I get a value printed for @machine,and @tblname1, but @command contains a NULL value and hence does not print.

However, if I test using this code:

SELECT @COMMAND = 'SELECT * INTO ' + @TBLNAME1 + ' FROM PCT_SNUGZ_FILTER WHERE MACHINE = ''' + @MACHINE1 + '''’
      print @machine
      print @tblname1
      print @command

I do get a value printed for @command:

SELECT * INTO premier_SO_MO FROM PCT_SNUGZ_FILTER WHERE MACHINE = 'premier'

So, the problem seems to be the part where I try to put in the @MO variable.  Can anyone tell me how to structure this dynamic SQL statement so the value in the @COMMAND variable ends up being:

'SELECT * INTO PREMIER_SO_MO FROM PCT_SNUGZ_FILTER WHERE MACHINE = ‘PREMIER’ AND LEFT([WORK ORDER NUMBER], 9) = 'MO-12345’

Thanks,

T
0
Comment
Question by:thoecherl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 35173896

This is only 8 characters
@MO = ‘MO-12345'

And you are using LEFT of 9 characters
LEFT([WORK ORDER NUMBER], 9) = 'MO-12345'
0
 

Author Closing Comment

by:thoecherl
ID: 35173927
How utterly embarrasing.  Thanks for catching my gaffe so quickly.

T
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question