简体   繁体   中英

SQL Sub-query parameters from Excel

I have SQL query with a sub-query where I want to assign sub query parameters from a cell in an Excel sheet, as this query cant be represented graphically Excel keep throwing error on this:

[Microsoft][ODBC SQL Server Driver] Invalid paramter number and [Microsoft][ODBC SQL Server Driver] Invalid Descriptor Index

I have already tried solution as mentioned here to trick excel without success Using Parameters in SQL query with sub-query

 ;WITH dataset_bl as (
SELECT 
--rank() over (partition by date_of_AC, ac_room order by  into_theatre) as OrderNumber
--,datetime_of_AC as booking_datetime
date_of_AC
,AC_room
,specialty
,OAMRN
,substring(Subject_NAME,1,CHARINDEX(' ',Subject_NAME,1)) as 'surname'
,substring(Subject_NAME,CHARINDEX(' ',Subject_NAME,1)+2,500) as 'forename'
,convert(date,[dttm_of_birth],103) as DOB
,PRIN_SO as 'surg'
,'"'+ASST_SO_1+' '+ASST_SO_2+'"' as 'assistant_SO'
,substring(CONSULT_NAME,1,abs(CHARINDEX(',',CONSULT_NAME,1)-1)) as 'consult_surname'
,substring(CONSULT_NAME,CHARINDEX(' ',CONSULT_NAME,1)+1,500) as 'consult_first_name'
,'"'+actual_AC+'"' as AC_details   
,'"'+PLANNED_PROD+'"' as booked_details
,'Carried Out' as 'TT_outcome'
, 'NULL' as  'unP_return_flag'
,OP_type
--,LOS
,case when CL_PRIORITY ='' then 'Not listed' else 'isted' end as islisted
,CLI_PRIORITY as listed_priority
,case when EM_PRIORITY like '<=%' then 'Submitted' else 'No Greensheet' end as isGreensheet
,em_priority as a, case when EM_PRIORITY like '<=%' and em_priority <>  '<=72hrsCannot charge before prod'then LEFT(EM_PRIORITY, abs(charindex('-',EM_PRIORITY)-1))  
 when em_priority = '<=72hrsCannot diso before prod' then '<=72hrs' else 'NULL' end as em_priority


FROM sample.dbo.tb_Fnl_Sur_th4 WITH (NOLOCK)
**WHERE main_ident = 'A224'  and  convert(date,into_Start, 103) >= '2019-07-01' and convert(date,into_Start, 103) <= '2019-07-31'** --i am trying to get these parameters from excel cell value
)
SELECT * FROM dataset_bl
WHERE specialty like 'abc%'
or (consult_surname like '%abc%' and consult_first_name like '%def%' )
    or surg in ('cde,fghi',
'jkl,'mnop,
'qrs,Tuv')


order by convert(date,date_of_procedure,103), operating_room--, into_theatre

Here's some suggestion.

First is to check the formatting of your date columns.

在此处输入图片说明

and build your parameter to be like

Cast(into_Start as smalldatetime) between ? and ?

I've been on that same issue and seem to find the solution for me. In fact the parameter [?] use in MSQUERY in Excel will normally work when you have a direct Query Ex:

Select [YourTable].[Column]
From   [YourTable]
Where  [YourTable].[Column]= ?

The Excel parameter will prompt and trigger where the windows that ask you which cell or Data you want that parameter have to be filled with.

In the other hand if you use the a Subquery and you add a Where condition with the Parameter ? will be stuck with the parameter [Microsoft][ODBC SQL Server Driver] Invalid paramter number and [Microsoft][ODBC SQL Server Driver] Invalid Descriptor Index

If you download the ODBC Driver for SQL Server here --> https://www.microsoft.com/fr-fr/download/details.aspx?id=56567

Install it and make sure that the ODBC use for your MSQUERY in Excel use that connection. ODBC Data Source to use

After this I was able to use a MSQuery in Excel using Parameter like this one

Select [RenamedTable].*
From  (Select         [YourTable].[Column1],
                      [YourTable].[Column2],
                      [YourTable].[Column3]
       From           [YourTable]) as RenamedTable
Where  [RenamedTable].[Column1] between ? and ?

Hope this will work for you as for me because I had similar problem with between date Parameter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM