querie with parameters: syntax error

khikyni

New Member
Joined
Aug 4, 2011
Messages
1
<table border="0" cellpadding="0" cellspacing="0" width="142"><colgroup><col style="mso-width-source:userset;mso-width-alt:5193;width:107pt" width="142"> </colgroup><tbody><tr style="height:15.0pt" height="20"> <td style="height:15.0pt;width:107pt" height="20" width="142">Hi all,
I´m using parameters in several SQL Server queries.</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">In the first one all is ok but in the second one i receive a syntax error.</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">
</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">Sucedeed querie:</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">--------------------------</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">select SourceMonitoringObjectDisplayName as 'Group Name',</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">TargetMonitoringObjectDisplayName as 'Group Members'</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">from RelationshipGenericView</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">where isDeleted=0</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">AND SourceMonitoringObjectDisplayName like 'Group Name'</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">ORDER BY TargetMonitoringObjectDisplayName</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">---------------------------</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">The steps i followed:</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">- excel 2007, add external data --> from SQL Server.</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">- i select the correct data source</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">- after creating the conection, i edit it and i select SQL "command type" and in "command text" I paste the query.</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">- I change the 'Group Name' with a ?</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">- I´m asked for the parameter and a select a cell in the book for updating it automatically</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">- all is fine, the group members are shown (this is a Querie to SCOM database) and when i change the cell, the new group members are also displayed.</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">------------------------</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">This is the unsucessful querie:</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">------------------------------------</td> </tr> <tr style="height:15.0pt" height="20"> <td class="xl63" style="height:15.0pt" height="20">SELECT vManagedEntity.DisplayName AS Computer</td> </tr> <tr style="height:15.0pt" height="20"> <td class="xl63" style="height:15.0pt" height="20">FROM vManagedEntity INNER JOIN</td> </tr> <tr style="height:15.0pt" height="20"> <td class="xl63" style="height:15.0pt" height="20"> vManagedEntityType ON vManagedEntity.ManagedEntityTypeRowId = vManagedEntityType.ManagedEntityTypeRowId</td> </tr> <tr style="height:15.0pt" height="20"> <td class="xl63" style="height:15.0pt" height="20">WHERE (vManagedEntity.TopLevelHostManagedEntityRowId IN</td> </tr> <tr style="height:15.0pt" height="20"> <td class="xl63" style="height:15.0pt" height="20"> (SELECT DISTINCT ME1.TopLevelHostManagedEntityRowId</td> </tr> <tr style="height:15.0pt" height="20"> <td class="xl63" style="height:15.0pt" height="20"> FROM vManagedEntity AS ME2 INNER JOIN</td> </tr> <tr style="height:15.0pt" height="20"> <td class="xl63" style="height:15.0pt" height="20"> vRelationship ON ME2.ManagedEntityRowId = vRelationship.SourceManagedEntityRowId INNER JOIN</td> </tr> <tr style="height:15.0pt" height="20"> <td class="xl63" style="height:15.0pt" height="20"> vManagedEntity AS ME1 ON vRelationship.TargetManagedEntityRowId = ME1.ManagedEntityRowId</td> </tr> <tr style="height:15.0pt" height="20"> <td class="xl63" style="height:15.0pt" height="20"> WHERE (ME2.DisplayName like 'Group Name'))) AND (vManagedEntityType.ManagedEntityTypeSystemName = 'Microsoft.SystemCenter.HealthService')</td> </tr> <tr style="height:15.0pt" height="20"> <td class="xl63" style="height:15.0pt" height="20">ORDER BY Computer</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">----------------------------------------</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">In this case, i receive a syntax error.</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">In both queries, if i fill 'group name' with text, the querie suceed.</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">But with parameter (?), the second querie fail with "syntax error".</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">
</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">Maybe excel has problems with parameters and this kind of syntax?</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">
</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">- A workaround i have in mind but i don´t know how to: i could use the first querie, this querie ask database A. But this querie goes into another query (derived table), and the principal query connects to database B. </td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">I don´t know if /how can i add two datasources in the same excel conection.</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">
</td> </tr> <tr style="height:15.0pt" height="20"> <td style="height:15.0pt" height="20">Thanks in advance.</td> </tr> </tbody></table>
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top