SQL VBS: query out into csv

Hello experts,

I am trying to query out into a csv file the following SQL query solved in http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28619120.html

SELECT UPPER (ISNULL(PARSENAME(org_3_levels, 2) + '.', '') + ISNULL(PARSENAME(org_3_levels, 1), '')) AS [Organization Revised], [Cardinality],sm.[SPoT Internal ID],[SPOT ID],[MSTT ID], s.Name as SPOTProjectName, m.Name MSTTProjectName FROM [ods].[IMP_CSV_SPOT_MSTT] sm
 left join [ods].[IMP_CSV_SPOT] s on s.[ID] =sm.[SPOT ID]
 left join [ods].[IMP_CSV_MSTT] m on m.[ID] =sm.[MSTT ID]
  CROSS APPLY (
      SELECT ISNULL(NULLIF(s.[Owning Organization], ''), m.[Owning Organization]) AS org_all_levels
  ) AS ca1
  CROSS APPLY (
      SELECT LEFT(org_all_levels, CHARINDEX('.', org_all_levels + '.', CHARINDEX('.', org_all_levels, CHARINDEX('.', org_all_levels) + 1) + 1) - 1) AS org_3_levels
  ) AS ca2

Open in new window


Through the vbs code provided in  http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28619120.html:


On error resume next
Dim data
Dim recordCount
Dim regEx
Set regEx = new RegExp
regEx.Pattern = "\r|\n|,|"""

Set con = CreateObject("ADODB.Connection")
con.Open "Driver={SQL Server};" & _ 
"Server=;" & _ 
"UID=;" & _ 
"PWD=;" & _ 
"Database=;" 

wscript.echo "CONNECTION OK" 

strQry = "SELECT UPPER (ISNULL(PARSENAME(org_3_levels, 2) + '.', '') + ISNULL(PARSENAME(org_3_levels, 1), '')) AS [Organization Revised], [Cardinality],sm.[SPoT Internal ID],[SPOT ID],[MSTT ID], s.Name as SPOTProjectName, m.Name MSTTProjectName FROM [ods].[IMP_CSV_SPOT_MSTT] sm
 left join [ods].[IMP_CSV_SPOT] s on s.[ID] =sm.[SPOT ID]
 left join [ods].[IMP_CSV_MSTT] m on m.[ID] =sm.[MSTT ID]
  CROSS APPLY (
      SELECT ISNULL(NULLIF(s.[Owning Organization], ''), m.[Owning Organization]) AS org_all_levels
  ) AS ca1
  CROSS APPLY (
      SELECT LEFT(org_all_levels, CHARINDEX('.', org_all_levels + '.', CHARINDEX('.', org_all_levels, CHARINDEX('.', org_all_levels) + 1) + 1) - 1) AS org_3_levels
  ) AS ca2"

Set data = con.execute(strQry)

Set filsSysObj = CreateObject("Scripting.FileSystemObject")
Set csvFile = filsSysObj.CreateTextFile("C:\Users\SESA249410\Desktop\Ongoing\test.csv", True)

recordCount = data.Fields.Count

strHeader = ""
Separator = ";"
For Each objField In data.Fields
	If strHeader = "" Then
		strHeader = objField.Name
	Else
		strHeader = strHeader & Separator & Replace(Replace(Replace(Replace(Replace(objField.Name, vbCrLf, ""), vbCr, ""), vbLf, ""), ",", ""), """", "")
	End If
Next
csvFile.WriteLine strHeader

Do until data.EOF
	strLine = ""
    For i = 0 To data.Fields.Count - 1
        Column = Replace(Replace(Replace(Replace(Replace(data.Fields( i ).Value, vbCrLf, ""), vbCr, ""), vbLf, ""), ";", ""), "", "")
        If strLine = "" Then
        	strLine = Column
        Else
        	strLine = strLine & Separator & Column
        End If
    Next
    csvFile.WriteLine strLine
    data.MoveNext
Loop

wscript.echo "Done"

Set data = Nothing
Set recordCount = Nothing
Set regEx= Nothing
Set con = Nothing
Set data = Nothing
Set filsSysObj = Nothing
Set csvFile = Nothing
Set objField = Nothing

Open in new window


However I am not able to query out the csv file with this complex sql query.
I got a VBScript Error code 800A0409, Unterminated string constant in line 18
If I do a simple select * from table the csv is properly generated which is not the case for the query above.

Someone can help me to know what should I modify or to provide me a sqlcmd or another vbscript to queryout this complex sql query?

Thank you in advance.
LVL 1
LD16Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omgangIT ManagerCommented:
If this is VBScript the error is telling you it doesn't like the statement on line 18
You need to modify how you're assigning the SQL statement to a variable

strQry = "SELECT UPPER (ISNULL(PARSENAME(org_3_levels, 2) + '.', '') + ISNULL(PARSENAME(org_3_levels, 1), '')) AS
 [Organization Revised], [Cardinality],sm.[SPoT Internal ID],[SPOT ID],[MSTT ID], s.Name as SPOTProjectName, m.Name MSTTProjectName FROM [ods].[IMP_CSV_SPOT_MSTT] sm

THIS IS LINE 18
 left join [ods].[IMP_CSV_SPOT] s on s.[ID] =sm.[SPOT ID]

 left join [ods].[IMP_CSV_MSTT] m on m.[ID] =sm.[MSTT ID]
  CROSS APPLY (
      SELECT ISNULL(NULLIF(s.[Owning Organization], ''), m.[Owning Organization]) AS org_all_levels
  ) AS ca1
  CROSS APPLY (
      SELECT LEFT(org_all_levels, CHARINDEX('.', org_all_levels + '.', CHARINDEX('.', org_all_levels, CHARINDEX('.', org_all_levels) + 1) + 1) - 1) AS org_3_levels
  ) AS ca2"

Everything needs to be wrapped in quotes.   Use line continuation characters

strQry = "SELECT UPPER (ISNULL(PARSENAME(org_3_levels, 2) + '.', '') + ISNULL(PARSENAME(org_3_levels, 1), '')) AS [Organization Revised], [Cardinality],sm.[SPoT Internal ID],[SPOT ID],[MSTT ID], s.Name as SPOTProjectName, m.Name MSTTProjectName FROM [ods].[IMP_CSV_SPOT_MSTT] sm " _
& "left join [ods].[IMP_CSV_SPOT] s on s.[ID] =sm.[SPOT ID] " _
& "left join [ods].[IMP_CSV_MSTT] m on m.[ID] =sm.[MSTT ID] " _
& "  CROSS APPLY ( " _
& "      SELECT ISNULL(NULLIF(s.[Owning Organization], ''), m.[Owning Organization]) AS org_all_levels " _
& "  ) AS ca1" _
& "  CROSS APPLY ( " _
& "      SELECT LEFT(org_all_levels, CHARINDEX('.', org_all_levels + '.', CHARINDEX('.', org_all_levels, CHARINDEX('.', org_all_levels) + 1) + 1) - 1) AS org_3_levels " _
& "  ) AS ca2"

Open in new window


OM Gang
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
omgangIT ManagerCommented:
If you look at lines 8 - 13 in your original script above you are using the same line continuation characters in the connection string statement.

OM Gang
0
LD16Author Commented:
OM Gang, thank you for this feedback, I will test it tomorrow.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

LD16Author Commented:
Hello I test it and it works.

I have an additional question, this  also apply for sqlcmd query out?

Because I am trying the code bellow and it is not working even with pause at the end the cmd open and close very quickly.

@echo off
sqlcmd -S server  -d db -U user  -P password-Q "SELECT UPPER(ISNULL(NULLIF(s.[Owning Organization], ''), m.[Owning Organization])) ,sm.[Cardinality], sm.[SPOT ID], sm.[MSTT ID]" _
& "from [ods].[IMP_CSV_SPOT_MSTT] sm left join [ods].[IMP_CSV_SPOT] s on s.[ID] =sm.[SPOT ID]" -o "spot.csv" -s";" -w 700 -W

Pause

Open in new window

0
omgangIT ManagerCommented:
@LD16, you're asking a second question; the first was VBScript related and the second is Windows CMD related.  You really should post in a separate question.  But.....

from the command window type TIMEOUT /?
I don't know why the PAUSE command isn't working for you but try TIMEOUT /T -1 instead and see if that works.

OM Gang
0
LD16Author Commented:
Hello,

I will create a new question. Thank you anyway for your feedback.

Regards,
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.