[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 571
  • Last Modified:

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
0
LeeHopkins
Asked:
LeeHopkins
  • 6
  • 4
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now