简体   繁体   中英

Is it possible to use excel cell value - as a reference in T-SQL statement?

I have table in SSMS (SQL Server Management Studio):

在此处输入图片说明

from which I need to delete data, based on the SurveyCode value.

Text part of the SurveyCode value CSS-2020-08- is stored in an Excel sheet, in B4 cell, file name Survey.xlsx :

在此处输入图片说明

And, for deleting / adding new data into my Survey table, I use the following code:

DELETE FROM [WH].[Fact].[Survey]
WHERE [WH].[Fact].[Survey].[SurveyCode] NOT LIKE '%2020-08%'

INSERT INTO
    [WH].Fact.[Survey]  (
          [SurveyCode]
         ,[SurveyDate]
         ,[Gender]
         ,[Age]
         ,[Questions]
         ,[Rating]
         ,[Score]
                       )
SELECT 
        staged.[SurveyCode]
       ,staged.[SurveyDate]
       ,staged.[Gender]
       ,staged.[Age]
       ,staged.[Question]
       ,staged.[Rating]
       ,staged.[Score]

FROM 
      [WH-SSIS].[WHCSS].[Staged_Survey] staged;

Is it possible - instead of using the SQL statement:

WHERE [WH].[Fact].[Survey].[SurveyCode] NOT LIKE '%2020-08%'

to reference CSS-2020-08- directly from the Excel spreadsheet - right inside the SQL statement?

I think

declare @parameter nvarchar(50)
SELECT parameter =f2
FROM OPENROWSET(
'Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;HDR=NO;Database=T:\temp\Survey.xlsx',
'select f2 from [sheet1$] where f1='''SurveyCode'''')

then you can use parameter in

WHERE [WH].[Fact].[Survey].[SurveyCode] NOT LIKE '%'+@parameter+'%'

For reading the xlsx file, first of all you will need to have the right provider installed, you can check that by:

USE [master];
EXEC sys.sp_enum_oledb_providers;

You should see Microsoft.ACE.OLEDB.XX.0, where XX can be typically either 12 or 16. If you don't see it, then you will need to install the provider. Find it from MS here .

Then, you will need to have 'Ad Hoc Distributed Queries' enabled. You can check it by running:

EXEC sys.sp_configure 'Ad Hoc Distributed Queries';

If above command does not work and you are admin, then you will need to run below commands:

USE [master];
EXEC sys.sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sys.sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;

It is also good to check some more settings:

USE master;
EXEC dbo.sp_MSset_oledb_prop;

See allow_in_process and dynamic_parameters values for Microsoft.ACE.OLEDB.16.0 on this example. If they are 0, you will need to enable them as well:

USE master;
EXEC dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.16.0', N'AllowInProcess', 1;
EXEC dbo.sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.16.0', N'DynamicParameters', 1;

After all above checks, be sure the user which is running SSMS has read permissions for the xlsx file you want to read.

Finally, you should be able to run a query like below with no issues:

DECLARE @parameter NVARCHAR(50);
SELECT @parameter = F2
    FROM OPENROWSET(
        'Microsoft.ACE.OLEDB.16.0',
        'Excel 8.0;HDR=NO;Database=D:\temp\Survey.xlsx',
        'select * from [sheet1$]') AS xlsx
    WHERE xlsx.F1 = 'SurveyCode';

And you can use the @parameter value as part of your query:

...NOT LIKE '%'+@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