IBM Client Access Express ODBC OpenQuery problem

I am still unable to pass a variable within an open query statement]
select * from openquery(newconn, 'Select
''@data''
from a db')

also is there a way to do date add within an open query
LeeHopkinsAsked:
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.

ShogunWadeCommented:
You cant do what you are trying (at least not that way)


try:

DECLARE @sql nvarchar(4000)

SET @Sql=N'SELECT * FROM OpenQuery(newconn,''SELECT  ' + @data + ' from mytable)'
EXEC sp_executesql @Sql
0
LeeHopkinsAuthor Commented:
nope got an error by the ) at the end invalid char

Declare @sql varchar(8000)
set @sql=N'Select * from openquery(as400new, ''Select
SUBSTRING (''09/22/2004'' , 1 , 2 ) as minth,
now() AS LASTDOWNLOADED,
A.MZORNO AS ORD_NUM,
C.MOPOC AS POLICYCODE,
C.MTYPN AS PARTTYPE,C.AISSCD AS ISSUECODE,
C.PMDMND AS DEMANDCODE,C.MCCL AS COM_CDE,
C.ACBAC AS PLN_CDE,
B.HZPPN AS PART, A.MZPLNT AS PLANT,
A.MZCO AS CMPY, A.MZWHSE AS WHSE,
A.MZCPN AS COMPONENT,A.MZCSEQ AS SEQNUM, A.MZSTC AS COMP_STAT,
A.MZTYPE AS COMP_TYP, B.HORDQ AS PARENT_QTY,
A.MZRQTY AS REQD_QTY, A.MZISCD AS ISS_CODE,
A.MZIQTY AS ISSUED, A.MZOPNO AS OP_NUM, A.MZRLVL AS ENG_LVL,
A.MZCSDT AS COMP_DUE, A.MZORST AS ORDER_STAT,
A.MZUSCD AS USAGE, A.MZSCFC AS SCRAPFAC,
A.MZQRTD AS QTY_RCVD, A.MZQNYR QTY_NOT_RCVD,  
C.PMMBCD AS BUYERCODE,C.CMDESC AS COMPONENT_DESC,
C.PMFAM AS FAM_ID, MLTCUM AS CUM_LT,
C.PMPDCL AS PROD_CLS,  C.PMMBFL AS COMP_M_B  
from IC120M A,
IC100M B,
DE100M C
WHERE B.HSTATS not in(''Z'',''C'') AND
C.MTYPN <>''5'' AND
A.MZSTC=''O'' AND
C.PMPLNT=''SGR'' AND
A.MZORNO=B.HZORNO AND
A.MZPLNT=C.PMPLNT AND
A.MZCPN=C.IZPN and
A.MZCSDT < 20041220')'
0
Duane LawrenceCommented:
DECLARE @sql nvarchar(4000)
       ,@data nvarchar(400)
set @data = 'dateadd(dd,1, column_name_here )'

SET @Sql=N'SELECT * FROM OpenQuery(newconn,''SELECT  ' + @data + ' from mytable)'''
select @sql

--EXEC sp_executesql @Sql

-- SELECT dateadd(dd,1,getdate())

ShogunWade is an outstanding programmer, he just missed a double quote.  He deserves partial credit.

Duane
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.

LeeHopkinsAuthor Commented:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'SELECT  mzcsdt from IC120M)'.
0
ShogunWadeCommented:
hope duane doesnt mind me correcting the dynamic sql :

SET @Sql=N'SELECT * FROM OpenQuery(newconn,''SELECT  ' + @data + ' from mytable'')'''

i think this will solve it.
0
LeeHopkinsAuthor Commented:
declare @sql nvarchar(4000)
declare @data varchar(10)
set @data = 'MZCSDT'

SET @Sql=N'SELECT * FROM OpenQuery(as400new,''SELECT  ' + @data + ' from IC120M'')'''

EXEC sp_executesql @Sql


Server: Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark before the character string ''.
0
ShogunWadeCommented:
can you put a PRINT @Sql immediately after the SET statement and send back the results  

ie:
declare @sql nvarchar(4000)
declare @data varchar(10)
set @data = 'MZCSDT'

SET @Sql=N'SELECT * FROM OpenQuery(as400new,''SELECT  ' + @data + ' from IC120M'')'''
PRINT @Sql
EXEC sp_executesql @Sql
0
LeeHopkinsAuthor Commented:
SELECT * FROM OpenQuery(as400new,'SELECT  MZCSDT from IC120M')'
0
ShogunWadeCommented:
ok,  its the last quote thats the problem.


try this:
declare @sql nvarchar(4000)
declare @data varchar(10)
set @data = 'MZCSDT'

SET @Sql=N'SELECT * FROM OpenQuery(as400new,''SELECT  ' + @data + ' from IC120M'')'
PRINT @Sql
EXEC sp_executesql @Sql
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
LeeHopkinsAuthor Commented:
ok now i have a problem with the some of the where clause
this it the query the result and error is at the bottom

Declare @STMonth as varchar(2)
declare @STZero as varchar(2)
Declare @stDay as varchar(2)
declare @stYear as varchar(4)
declare @stDate as varchar(8)
Declare @Today as smalldatetime

Set @Today = dateadd(d, +90, getdate())

set @STZero = '0'

set @stMonth =
       case
      when len(datepart(m,@Today)) = 1 then @STZero + cast(datepart(m,@Today)as varchar)
      else cast(datepart(m,@Today)as varchar)
end
Set @stday =
      case
      when len(datepart(d,@Today)) = 1 then @STZero + cast(datepart(d,@Today)as varchar)
      else cast(datepart(d,@Today)as varchar)
end
Set @stYear = cast(datepart(yyyy,@Today)as varchar)
set @stDate = @stYear + @stMonth + @stday
print @stdate
declare @sql nvarchar(4000)


set @sql = N'Select * from openquery(as400new,''Select
now() AS LASTDOWNLOADED,
A.MZORNO AS ORD_NUM,
C.MOPOC AS POLICYCODE,
C.MTYPN AS PARTTYPE,C.AISSCD AS ISSUECODE,
C.PMDMND AS DEMANDCODE,C.MCCL AS COM_CDE,
C.ACBAC AS PLN_CDE,
B.HZPPN AS PART, A.MZPLNT AS PLANT,
A.MZCO AS CMPY, A.MZWHSE AS WHSE,
A.MZCPN AS COMPONENT,A.MZCSEQ AS SEQNUM, A.MZSTC AS COMP_STAT,
A.MZTYPE AS COMP_TYP, B.HORDQ AS PARENT_QTY,
A.MZRQTY AS REQD_QTY, A.MZISCD AS ISS_CODE,
A.MZIQTY AS ISSUED, A.MZOPNO AS OP_NUM, A.MZRLVL AS ENG_LVL,
A.MZCSDT AS COMP_DUE, A.MZORST AS ORDER_STAT,
A.MZUSCD AS USAGE, A.MZSCFC AS SCRAPFAC,
A.MZQRTD AS QTY_RCVD, A.MZQNYR QTY_NOT_RCVD,  
C.PMMBCD AS BUYERCODE,C.CMDESC AS COMPONENT_DESC,
C.PMFAM AS FAM_ID, MLTCUM AS CUM_LT,
C.PMPDCL AS PROD_CLS,  C.PMMBFL AS COMP_M_B  
from IC120M A,
IC100M B,
DE100M C
WHERE B.HSTATS not in(''Z'',''C'') AND
C.MTYPN <>''5'' AND
A.MZSTC=''O'' AND
C.PMPLNT=''SGR'' AND
A.MZORNO=B.HZORNO AND
A.MZPLNT=C.PMPLNT AND
A.MZCPN=C.IZPN and
A.MZCSDT < ' + @stdate + ''')'
print @sql
exec sp_executesql @sql



******************Result and error

Select * from openquery(as400new,'Select
now() AS LASTDOWNLOADED,
A.MZORNO AS ORD_NUM,
C.MOPOC AS POLICYCODE,
C.MTYPN AS PARTTYPE,C.AISSCD AS ISSUECODE,
C.PMDMND AS DEMANDCODE,C.MCCL AS COM_CDE,
C.ACBAC AS PLN_CDE,
B.HZPPN AS PART, A.MZPLNT AS PLANT,
A.MZCO AS CMPY, A.MZWHSE AS WHSE,
A.MZCPN AS COMPONENT,A.MZCSEQ AS SEQNUM, A.MZSTC AS COMP_STAT,
A.MZTYPE AS COMP_TYP, B.HORDQ AS PARENT_QTY,
A.MZRQTY AS REQD_QTY, A.MZISCD AS ISS_CODE,
A.MZIQTY AS ISSUED, A.MZOPNO AS OP_NUM, A.MZRLVL AS ENG_LVL,
A.MZCSDT AS COMP_DUE, A.MZORST AS ORDER_STAT,
A.MZUSCD AS USAGE, A.MZSCFC AS SCRAPFAC,
A.MZQRTD AS QTY_RCVD, A.MZQNYR QTY_NOT_RCVD,  
C.PMMBCD AS BUYERCODE,C.CMDESC AS COMPONENT_DESC,
C.PMFAM AS FAM_ID, MLTCUM AS CUM_LT,
C.PMPDCL AS PROD_CLS,  C.PMMBFL AS COMP_M_B  
from IC120M A,
IC100M B,
DE100M C
WHERE B.HSTATS not in('Z','C') AND
C.MTYPN <>'5' AND
A.MZSTC='O' AND
C.PMPLNT='SGR' AND
A.MZORNO=B.HZORNO AND
A.MZPLNT=C.PMPLNT AND
A.MZCPN=C.IZPN and
A.MZCSDT < 20050221')
Server: Msg 170, Level 15, State 1, Line 23
Line 23: Incorrect syntax near 'Z'.
0
LeeHopkinsAuthor Commented:
got it had to add ''''z'''',''''c'''' around all my strings
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
Microsoft SQL Server

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.