Showing posts with label varchar. Show all posts
Showing posts with label varchar. Show all posts

Friday, March 30, 2012

OpenQuery using a variable

Hi,

Here's what I did:

1) I declared a new VARCHAR(2000) variable called CQUERY like this:
DECLARE @.CQUERY VARCHAR(2000)
2) I put a string query in the variable:
SET @.CQUERY = 'SELECT ...'

Now, when I try to execute the OpenQuery method using that variable, it fails.

Here's the call:
SELECT * FROM OPENQUERY(OracleSource, @.CQUERY)

I get the following error:
Server: Msg 170, Level 15, State 1, Line 13
Line 13: Incorrect syntax near '@.CQUERY'.

Don't tell me I can't use a variable instead of a static query? What am I doing wrong?

Thanks,

Skip.i don't think you can do that, putting a variable in the from clause

you'll have to use dynamic sql

so put that statement in a EXEC(.....)|||Alright,

I tried it but I'm still having troubles with it. Here's my code (simplified version):

DECLARE @.CQUERY
SET @.CQUERY = 'SELECT * FROM OPENQUERY(OracleSource, ' + '''' + 'SELECT * FROM mytable WHERE last_name = ' + '''' + 'DOE' + '''' + '''' + ')'
EXECUTE(@.CQUERY)

When parsing, it's fine but at execution, it fails which is normal because it tries to execute the following query:

SELECT * FROM OPENQUERY(OracleSource, 'SELECT * FROM mytable WHERE last_name = 'DOE'')

It's, of course, incorrect because the query string stops before DOE because there's an apostrophy there so the system tries to execute the following query:

SELECT * FROM mytable WHERE last_name =

which is incorrect.

Any other suggestions?

Thanks again,

Skip.|||sorry if i mislead you the first time, what i meant is use EXEC if you are going to use a variable for openquery.

if you are not using a variable for openquery, then just do this:
SELECT * FROM OPENQUERY(OracleSource, 'SELECT * FROM mytable WHERE last_name = ''DOE''')|||Originally posted by Skippy_sc
Alright,

I tried it but I'm still having troubles with it. Here's my code (simplified version):

DECLARE @.CQUERY
SET @.CQUERY = 'SELECT * FROM OPENQUERY(OracleSource, ' + '''' + 'SELECT * FROM mytable WHERE last_name = ' + '''' + 'DOE' + '''' + '''' + ')'
EXECUTE(@.CQUERY)

When parsing, it's fine but at execution, it fails which is normal because it tries to execute the following query:

SELECT * FROM OPENQUERY(OracleSource, 'SELECT * FROM mytable WHERE last_name = 'DOE'')

It's, of course, incorrect because the query string stops before DOE because there's an apostrophy there so the system tries to execute the following query:

SELECT * FROM mytable WHERE last_name =

which is incorrect.

Any other suggestions?

Thanks again,

Skip.

Try this for instance:

DECLARE @.CQUERY varchar(8000)
SET @.CQUERY = 'SELECT * FROM OPENQUERY(MSSQL20,
''SELECT top 10 * FROM master.dbo.sysobjects where name=''+'sysobjects'+'')'
select @.CQUERY
EXECUTE(@.CQUERY)|||Thank you very much fattyacid, it works fine now!

Skipsql

OpenQuery Syntax Using Variables

I have an openquery statement with a parameter embeded as a variable:

declare @.product varchar(3)

set @.product= 'ABC'

select * from openquery(SomeServer,'

SELECT Description, Size

FROM Products

WHERE

Group = ''XY'' AND

Code = ''' + @.product + '''')

When I run it I get the following message:

Msg 102, Level 15, State 1, Line 8

Incorrect syntax near '+'.

When I hard code the "Code" value, like so:

Code = ''ABC''')

...it works fine.

I am at a loss and would appreciate any help on this.

Thanks in advance

SQL Servant

Can you try SET QUOTED_IDENTIFIER OFF?

cheers,

Andrew

|||

I copied your code into Query Analyzer, same error too

It must be the quotation, do you want " or ' enclosing your @.product?

e.g. you want "Drink", or 'Drink'?

This is my code for some script that uses OpenQuery, I think I had to use EXEC to run it for the same problem you had (it won't take +)

Code Snippet

EXEC ('SELECT * FROM OPENQUERY(SERVER, ''SELECT * FROM TABLE WHERE Table_Id = ' + @.Table_Id_Str + ''')')

|||

Tried it... same error.

Thanks,

SQL Servant

Reply to --

Can you try SET QUOTED_IDENTIFIER OFF?

cheers,

Andrew

|||

The query sent to the server needs to have 'ABC' rather than "ABC".

I have used EXEC before and got the same error. Anyway, this query is part of an IF structure...

IF EXISTS (select * from openquery(...))

[do this ] ELSE [do that]

Thanks,

SQL Servant

Reply to -

I copied your code into Query Analyzer, same error too

It must be the quotation, do you want " or ' enclosing your @.product?

e.g. you want "Drink", or 'Drink'?

This is my code for some script that uses OpenQuery, I think I had to use EXEC to run it for the same problem you had (it won't take +)

|||

I have fixed the problem...

Apparently it is to do with scope and stuff...

If I put the query in a EXEC command then it works. So, the code becomes this:

EXEC('

select * from openquery(SomeServer,''

SELECT Description, Size

FROM Products

WHERE

Group = ''''XY'''' AND

Code = ''''' + @.product + ''''''')

')

In fact, I have put the entire IF structure (that this query is a part of) inside an EXEC command.

Over and out,

SQL Servant

|||

Wasn't that the idea of what I posted? ;-P

I think I deserve a star, hee hee

Anyway, please mark this thread as Answered

Glad it worked out for you

Wednesday, March 28, 2012

openquery

BOL says that 'OPENQUERY does not accept variables for its
arguments'. So, I tried using something like:
Declare @.qry varchar(1000)
Set @.qry = 'select * from OPENQUERY
(<linked_server_name>, ''<query>'')'
exec (@.qry)
Would this work? Thanks.Yes.
--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net
"Rob" <anonymous@.discussions.microsoft.com> wrote in message
news:018301c3ad3f$50936e10$a401280a@.phx.gbl...
> BOL says that 'OPENQUERY does not accept variables for its
> arguments'. So, I tried using something like:
> Declare @.qry varchar(1000)
> Set @.qry = 'select * from OPENQUERY
> (<linked_server_name>, ''<query>'')'
> exec (@.qry)
> Would this work? Thanks.|||That is called Dynamic SQL and should work if the final syntax comes out
correct.
--
Geoff N. Hiten
Microsoft SQL Server MVP
Senior Database Administrator
Careerbuilder.com
"Rob" <anonymous@.discussions.microsoft.com> wrote in message
news:018301c3ad3f$50936e10$a401280a@.phx.gbl...
> BOL says that 'OPENQUERY does not accept variables for its
> arguments'. So, I tried using something like:
> Declare @.qry varchar(1000)
> Set @.qry = 'select * from OPENQUERY
> (<linked_server_name>, ''<query>'')'
> exec (@.qry)
> Would this work? Thanks.

Friday, March 23, 2012

OPENDATASOURCE

Hi all,
Why I can't do the following:
DECLARE @.stringConnection as varchar(150)
set @.stringConnection= 'Data Source=vrpims\cpims'+';'+'User
ID=sa'+';'+'Password=sa'
select @.maxAttendancesVC= max(AEATT_REFNO)
from OPENDATASOURCE( 'SQLOLEDB',
@.stringConnection).materprod.dbo.ae_attendances
When I try to execute the above coding the following error message comes out
:
Server: Msg 170, Level 15, State 1, Line 25
Line 25: Incorrect syntax near '@.stringConnection'.Hi
You can not use a variable as the init_string parameter. If you really need
to do this then use dynamic SQL although your example implies that creating
a linked server may be more suitable.
John
"FJC" <FJC@.discussions.microsoft.com> wrote in message
news:D1504C11-BCD1-40F0-8E88-40AB9DCE1522@.microsoft.com...
> Hi all,
> Why I can't do the following:
> DECLARE @.stringConnection as varchar(150)
> set @.stringConnection= 'Data Source=vrpims\cpims'+';'+'User
> ID=sa'+';'+'Password=sa'
> select @.maxAttendancesVC= max(AEATT_REFNO)
> from OPENDATASOURCE( 'SQLOLEDB',
> @.stringConnection).materprod.dbo.ae_attendances
> When I try to execute the above coding the following error message comes
> out:
> Server: Msg 170, Level 15, State 1, Line 25
> Line 25: Incorrect syntax near '@.stringConnection'.
>|||Read more about Dynamic SQL
http://www.sommarskog.se/dynamic_sql.html
Madhivanan|||Thanks for the info mates
"Madhivanan" wrote:

> Read more about Dynamic SQL
> http://www.sommarskog.se/dynamic_sql.html
> Madhivanan
>

OPEN XML QUESTION

DECLARE @.MetaTagXML XML
declare @.XmlDocumentHandler INT
DECLARE @.QuestionName TABLE (
Id INT IDENTITY(1,1),
[metatag] [Varchar](100))
SELECT @.MetaTagXML ='<Report ReportGUID
="CBFF8200-3B52-4F24-9D48-AC5458720B41">
<MetaTags>
<MetaTag>Awareness</MetaTag>
<MetaTag>advertising</MetaTag>
</MetaTags>
<Variables>
<Variable NodeID="7700">
<MetaTags>
<MetaTag>Evaluation</MetaTag>
<MetaTag>account management/primary source experience</MetaTag>
</MetaTags>
</Variable>
<Variable NodeID="7701">
<MetaTags>
<MetaTag>quality</MetaTag>
<MetaTag>quality of account management</MetaTag>
</MetaTags>
</Variable>
</Variables>
</Report>'
EXECUTE SP_XML_PREPAREDOCUMENT @.XmlDocumentHandler OUTPUT, @.MetaTagXML
SELECT *
FROM OPENXML (@.XmlDocumentHandler,
'/Report/Variables/Variable/MetaTags',2)
WITH (
MetaTag varchar(100),
NodeID varchar(100) '../@.NodeID'
)
EXECUTE SP_XML_REMOVEDOCUMENT @.XmlDocumentHandler
i am getting the resultset like this:
Evaluation 7700
quality 7701
I expect like this
Evaluation 7700 CBFF8200-3B52-4F24-9D48-AC5458720B41
account management/primary source experience 7700
CBFF8200-3B52-4F24-9D48-AC5458720B41
quality 7701 CBFF8200-3B52-4F24-9D48-AC5458720B41
quality of account management 7701 CBFF8200-3B52-4F24-9D48-AC5458720B41
1. how to get all records
2. how to reference reportguid
Thanks in advance
DevaYou want one row per MetaTag element, so your row pattern in OpenXML needs
to select that element.
And you need to add a row for the ReportGUID:
SELECT *
FROM OPENXML (@.XmlDocumentHandler,
'/Report/Variables/Variable/MetaTags/MetaTag',2)
WITH (
MetaTag varchar(100) '.',
NodeID varchar(100) '../../@.NodeID',
ReportID varchar(100) '../../../../@.ReportGUID'
)
Also note that since you are using OpenXML, using the XML datatype is not
necessarily beneficial, since sp_xml_preparedocument will serialize the XML
and reparse it. So using nvarchar(max) may be the better approach to pass
the XML data to the parser.
Best regards
Michael
"xmldev" <xmldev@.discussions.microsoft.com> wrote in message
news:8BD7E081-246C-47A8-8A53-C6FBDD157786@.microsoft.com...
> DECLARE @.MetaTagXML XML
> declare @.XmlDocumentHandler INT
> DECLARE @.QuestionName TABLE (
> Id INT IDENTITY(1,1),
> [metatag] [Varchar](100))
>
> SELECT @.MetaTagXML ='<Report ReportGUID
> ="CBFF8200-3B52-4F24-9D48-AC5458720B41">
> <MetaTags>
> <MetaTag>Awareness</MetaTag>
> <MetaTag>advertising</MetaTag>
> </MetaTags>
> <Variables>
> <Variable NodeID="7700">
> <MetaTags>
> <MetaTag>Evaluation</MetaTag>
> <MetaTag>account management/primary source experience</MetaTag>
> </MetaTags>
> </Variable>
> <Variable NodeID="7701">
> <MetaTags>
> <MetaTag>quality</MetaTag>
> <MetaTag>quality of account management</MetaTag>
> </MetaTags>
> </Variable>
> </Variables>
> </Report>'
> EXECUTE SP_XML_PREPAREDOCUMENT @.XmlDocumentHandler OUTPUT, @.MetaTagXML
> SELECT *
> FROM OPENXML (@.XmlDocumentHandler,
> '/Report/Variables/Variable/MetaTags',2)
> WITH (
> MetaTag varchar(100),
> NodeID varchar(100) '../@.NodeID'
> )
> EXECUTE SP_XML_REMOVEDOCUMENT @.XmlDocumentHandler
> i am getting the resultset like this:
> Evaluation 7700
> quality 7701
> I expect like this
>
> Evaluation 7700 CBFF8200-3B52-4F24-9D48-AC5458720B41
> account management/primary source experience 7700
> CBFF8200-3B52-4F24-9D48-AC5458720B41
> quality 7701 CBFF8200-3B52-4F24-9D48-AC5458720B41
> quality of account management 7701 CBFF8200-3B52-4F24-9D48-AC5458720B41
> 1. how to get all records
> 2. how to reference reportguid
> Thanks in advance
> Deva
>