Openrowset with Spaces in source - database

I am attempting to load an Excel file into SQL Server using the OPENROWSET and I think I have narrowed my issue down to there being spaces in the file name. I am using the following Stored Procedure where the source is dynamically passed in:
USE [MyDB]
GO
/****** Object: StoredProcedure [dbo].[SP_LOAD_EXCEL] Script Date: 08/07/2014 10:22:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--Creates a Store Procedure to take a fully qualified file name and
--load to a temporary database
ALTER PROCEDURE [dbo].[SP_LOAD_EXCEL]
#file nvarchar(max)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #Sql Nvarchar(max);
SET #Sql = 'INSERT INTO [MyDB].[dbo].[ExcelData]
SELECT *
FROM OPENROWSET (''Microsoft.Ace.OLEDB.12.0''
,''Excel 12.0; Database='+#file +';Extended Properties=''''EXCEL 12.0;HDR=NO;IMEX=1''
,''SELECT * FROM [Sheet1$]'')';
EXEC(#Sql);
END
I am calling the SP with the following and this works:
USE [MyDB]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[SP_LOAD_EXCEL]
#file = N'E:\My_Finalised.xlsx'
SELECT 'Return Value' = #return_value
GO
However if I substitute the filename for E:\Database backups\My_Finalised.xlsx then it doesnt work.
So to summarize:
WORKS: E:\My_Finalised.xlsx
DOESNT WORK: E:\Database backups\My_Finalised.xlsx
How do I get OPENROWSET to accept a source that has spaces in it? Thanks

Related

How to pass decryption certificate password as parameter to stored procedure

Using SQL Server 2012 v11.0.2100.60, I am trying to create a stored procedure that opens a symmetric key with decryption by certificate with password.
If I put the hard-coded password in the stored procedure it works fine, but I would like to be able to pass the certificate password as a parameter to the stored procedure (so that the plaintext pw doesn't live anywhere on the server).
Here's what I've tried.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[testSproc]
#pw VARCHAR(50)
AS
BEGIN
SET NOCOUNT ON;
OPEN SYMMETRIC KEY key_name
DECRYPTION BY CERTIFICATE cert_name WITH PASSWORD = '''+#pw+''';
SELECT ....
When I execute I get the following:
Msg 15466, Level 16, State 9, Procedure testSproc, Line 9
An error occurred during decryption.
This is what it looks like when I execute:
DECLARE #return_value int
EXEC #return_value = [dbo].[testSproc]
#GL = N'98787notTheRealPW45668456318'
-- I've tried both N'98787notTheRealPW45668456318' and just '98787notTheRealPW45668456318'
SELECT 'Return Value' = #return_value
GO
Am I passing the parameter incorrectly? Or setting the variable in the stored procedure incorrectly? Any help is much appreciated!
Figured it out. Put the OPEN KEY command into it's own variable and pass in the pw there. Then just execute.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[testSproc]
#pw NVARCHAR(max)
AS
BEGIN
DECLARE #SQL AS NVARCHAR(MAX);
SET NOCOUNT ON;
SET #SQL = 'OPEN SYMMETRIC KEY key_name DECRYPTION BY CERTIFICATE cert_name WITH PASSWORD = '''+#pw+'''';
EXEC (#SQL)
SELECT ....

ODBC vs. OLEDB for calling SQL Server stored procedures from Alteryx

I have the following stored procedure in SQL Server. If I run it using OLEDB in Alteryx, I get the correct result set. However, if I run it using ODBC in Alteryx, I get this error msg:
No Columns Returned
How should I make this stored procedure work with ODBC?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Saqib
-- Create date: 07/06/18
-- Description: Test of OLEDB vs. ODBC
-- =============================================
ALTER PROCEDURE [dbo].[test_sp]
AS
BEGIN
SET NOCOUNT ON;
DECLARE #SQL VARCHAR(1000)
SELECT #SQL = 'SELECT * FROM test_table'
EXEC (#SQL)
END

i want select city from cites table and create View in stored Procedure

my stored procedure :
USE [maskanjo.com_travelenterDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[SPSelectCites]
-- Add the parameters for the stored procedure here
#cityName nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
--------------------------Select---------------------
CREATE VIEW CUSTOMERS_VIEW AS
SELECT *
FROM [dbo].[City] where [NameFA] LIKE cityName+'%'
-----------------------------------------------------
END
i want select city from cites table and create View
have error
Error :
Incorrect syntax:'CREATE VIEW' Must be only statement in the batch
The error tells you what the problem is. Try executing the query using the "Exec" command:
declare #sql varchar(max)
set #sql = 'CREATE VIEW CUSTOMERS_VIEW AS SELECT * FROM [dbo].[City]
where [NameFA] LIKE ''' + cityName + '%'''
exec (#sql)

Execute SSIS in SSMS

I have a SSIS package which will upload files into tables. I want to execute it as soon as a file has been uploaded and saved to a table. This link showed how to execute it using Stored Procedure. What I did was I created a trigger with the following code:
ALTER TRIGGER [dbo].[tr_ImportFile]
ON [dbo].[ReconMedicalAidFile]
AFTER INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
DECLARE #params VARCHAR(MAX),
#ssisStr VARCHAR(MAX),
#packageName VARCHAR(MAX),
#serverName VARCHAR(MAX)
SET #serverName = 'ServerName'
SET #packageName = 'MyIntegration'
SET #ssisStr = 'dtexec /sq ' + #packageName + ' /ser ' + #serverName
DECLARE #returnCode int
EXEC #returnCode = xp_cmdshell #ssisStr
END
I get the following error Procedure expects parameter 'command_string' of type 'varchar'.
Any help would be appreciated.
Thanks
I think it works when you replace VARCHAR(MAX) with VARCHAR(8000) data types
I went a different route. Still have the trigger but it will execute a SQL Job which contains the SSIS package.
EXEC msdb.dbo.sp_start_job N'JobName' ;

How to use a variable for the database name in T-SQL?

I use the database name in several places in my script, and I want to be able to quickly change it, so I'm looking for something like this:
DECLARE #DBNAME VARCHAR(50)
SET #DBNAME = 'TEST'
CREATE DATABASE #DBNAME
GO
ALTER DATABASE #DBNAME SET COMPATIBILITY_LEVEL = 90
GO
ALTER DATABASE #DBNAME SET RECOVERY SIMPLE
GO
But it doesn't work. So what's the correct way to write this code?
Put the entire script into a template string, with {SERVERNAME} placeholders. Then edit the string using:
SET #SQL_SCRIPT = REPLACE(#TEMPLATE, '{SERVERNAME}', #DBNAME)
and then run it with
EXECUTE (#SQL_SCRIPT)
It's hard to believe that, in the course of three years, nobody noticed that my code doesn't work!
You can't EXEC multiple batches. GO is a batch separator, not a T-SQL statement. It's necessary to build three separate strings, and then to EXEC each one after substitution.
I suppose one could do something "clever" by breaking the single template string into multiple rows by splitting on GO; I've done that in ADO.NET code.
And where did I get the word "SERVERNAME" from?
Here's some code that I just tested (and which works):
DECLARE #DBNAME VARCHAR(255)
SET #DBNAME = 'TestDB'
DECLARE #CREATE_TEMPLATE VARCHAR(MAX)
DECLARE #COMPAT_TEMPLATE VARCHAR(MAX)
DECLARE #RECOVERY_TEMPLATE VARCHAR(MAX)
SET #CREATE_TEMPLATE = 'CREATE DATABASE {DBNAME}'
SET #COMPAT_TEMPLATE='ALTER DATABASE {DBNAME} SET COMPATIBILITY_LEVEL = 90'
SET #RECOVERY_TEMPLATE='ALTER DATABASE {DBNAME} SET RECOVERY SIMPLE'
DECLARE #SQL_SCRIPT VARCHAR(MAX)
SET #SQL_SCRIPT = REPLACE(#CREATE_TEMPLATE, '{DBNAME}', #DBNAME)
EXECUTE (#SQL_SCRIPT)
SET #SQL_SCRIPT = REPLACE(#COMPAT_TEMPLATE, '{DBNAME}', #DBNAME)
EXECUTE (#SQL_SCRIPT)
SET #SQL_SCRIPT = REPLACE(#RECOVERY_TEMPLATE, '{DBNAME}', #DBNAME)
EXECUTE (#SQL_SCRIPT)
You can also use sqlcmd mode for this (enable this on the "Query" menu in Management Studio).
:setvar dbname "TEST"
CREATE DATABASE $(dbname)
GO
ALTER DATABASE $(dbname) SET COMPATIBILITY_LEVEL = 90
GO
ALTER DATABASE $(dbname) SET RECOVERY SIMPLE
GO
EDIT:
Check this MSDN article to set parameters via the SQLCMD tool.
Unfortunately you can't declare database names with a variable in that format.
For what you're trying to accomplish, you're going to need to wrap your statements within an EXEC() statement. So you'd have something like:
DECLARE #Sql varchar(max) ='CREATE DATABASE ' + #DBNAME
Then call
EXECUTE(#Sql) or sp_executesql(#Sql)
to execute the sql string.
You cannot use a variable in a create table statement. The best thing I can suggest is to write the entire query as a string and exec that.
Try something like this:
declare #query varchar(max);
set #query = 'create database TEST...';
exec (#query);

Resources