Create assembly using stored procedure - sql-server

I want to create an assembly in SQL Server 2008 R2 if it does not exist in the specific database by using a stored procedure, giving the path of the file as a parameter.
USE DBName
GO
CREATE PROCEDURE spCreateAssembly
#FilePath varchar(max)
AS
BEGIN
IF NOT EXISTS (select 1 from sys.assembly_files f
full outer join sys.assemblies a on f.assembly_id=a.assembly_id
full outer join sys.assembly_modules m on a.assembly_id=m.assembly_id
WHERE a.name = 'Lib1')
BEGIN
sp_configure 'show advanced options', 1
RECONFIGURE
GO
sp_configure 'clr enabled', 1
RECONFIGURE
GO
sp_configure 'show advanced options', 0
RECONFIGURE
GO
Create Assembly Lib1 from #FilePath with Permission_set = SAFE
END
END
But getting an error:
Msg 102, Level 15, State 1, Procedure spCreateAssembly, Line 14
Incorrect syntax near 'sp_configure'.

Try this:
CREATE PROCEDURE spCreateAssembly
#FilePath varchar(max)
AS
BEGIN
IF NOT EXISTS (select 1 from sys.assembly_files f
full outer join sys.assemblies a on f.assembly_id=a.assembly_id
full outer join sys.assembly_modules m on a.assembly_id=m.assembly_id
WHERE a.name = 'Lib1')
BEGIN
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'clr enabled', 1
RECONFIGURE
EXEC sp_configure 'show advanced options', 0
RECONFIGURE
Create Assembly Lib1 from #FilePath with Permission_set = SAFE
END
END
You cannot use GO in stored procedure.

Related

Creating Excel files with OPENROWSET

Following my previous question, Previous Question, I have reached till this point using sql-server, but I cannot create the excel files in my directory. Can anyone help me to create excel files? ( I need to create dynamic excel files for example: First 500k --> 1.xlsx; Second 500k --> 2.xlsx and so on.) Attached you will find my T-SQL code.
Use Master
GO
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1
GO
USE [AdventureWorksDW2019]
GO
DECLARE #PageNumber AS INT
DECLARE #ExcelFileName as nVarchar(60)
SET #ExcelFileName = cast(#PageNumber as nvarchar)
DECLARE #RowsOfPage AS INT
DECLARE #MaxTablePage AS FLOAT
SET #PageNumber=1
SET #RowsOfPage=500000
SELECT #MaxTablePage = COUNT(CustomerKey)
FROM [dbo].[DimCustomer]
SET #MaxTablePage = CEILING(#MaxTablePage/#RowsOfPage)
WHILE #MaxTablePage >= #PageNumber
BEGIN
SELECT *
FROM [dbo].[DimCustomer]
ORDER BY CustomerKey
OFFSET (#PageNumber-1)*#RowsOfPage ROWS
FETCH NEXT #RowsOfPage ROWS ONLY
DECLARE #FileName nvarchar(400) = #ExcelFileName+ '.xlsx'
DECLARE #FullFileName nvarchar(400) = 'C:\Users\Desktop\CHANGE_LOGS_FOLDER\'+#FileName
DECLARE #sql nvarchar(max)
SET #sql = 'INSERT INTO OPENROWSET(''Microsoft.ACE.OLEDB.12.0'',''Excel 12.0;Database=' +#FullFileName+''', ''SELECT * FROM [Sheet1$]'')
SELECT *
FROM [dbo].[DimCustomer] '''+#ExcelFileName+''''
EXEC (#sql)
SET #PageNumber = #PageNumber + 1
END
Use Master
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 0
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure 'ad hoc distributed queries', 0
RECONFIGURE
GO
EXEC master.dbo.sp_configure 'show advanced options', 0
RECONFIGURE WITH OVERRIDE
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 0
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 0
GO

How to run this SQL statement sp_configure 'show advanced options', 1; in Crystal report?

This is code does not run in the Crystal Report.
Error:Failed to retrieve data from the database. Details: ADO Error Code 0x80040e14 Source Microsoft SQL Server Native Client 11.0 Description: Incorrect syntax near isp_configure. SQL State 42000 Native Error: 102 [Database Vendor Code: 102
Declare #ReportDate DateTime
Set #ReportDate={?ReportDateR}
Select * from [SBO_SPEL].[dbo].[U_SPEL_SALES_ANALYSIS_TARGET&ACTUAL_UNIT_F] (#ReportDate)
UNION ALL
Select * from [SBO_SPEL].[dbo].[U_SPEL_SALES_ANALYSIS_TARGET&ACTUAL_SE_F] (#ReportDate)
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'max server memory', 32768;
GO
RECONFIGURE;
GO
WAITFOR DELAY '00:02:00'
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'max server memory', 131072;
GO
RECONFIGURE;
GO
Crystal returns to the report the result set from the last SQL statement in a Command or SP. But your last SQL statement has no result set.

master..xp_cmdshell is blockes on SQL Server

How to enable the
xp_cmdshell and why is it blocked or disabled?*
while enabling you need Check out the Permission section of the
xp_cmdshell MSDN docs:
please follow link
https://msdn.microsoft.com/en-us/library/ms190693.aspx
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
xp_cmdshell- Executes a given command string or batch file as an operating-system command shell and returns any output as rows of text.
Permission/Rights: Only SysAdmin fixed role can execute it.
Syntax
xp_cmdshell {‘command_string‘} [, no_output]
Arguments
‘command_string‘
Is the command string to execute at the operating-system command shell or from DOS prompt. command_string is varchar(255) or nvarchar(4000), with no default.
command_string cannot contain more than one set of double quotation marks.
A single pair of quotation marks is necessary if any spaces are present in the file paths or program names referenced by command_string.
If you have trouble with embedded spaces, consider using FAT 8.3 file names as a workaround.
no_output
Is an optional parameter executi
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- Disable xp_cmdshell option now
EXEC sp_configure 'xp_cmdshell', 0
GO
RECONFIGURE
GO
FOr More Info-- http://yrushka.com/index.php/sql-server/security/execute-remotely-t-sql-command-through-xp_cmdshell/
Use Master
GO
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
GO

Using query export SQL Server 2008 table to Excel Sheet

How to export sql server table data to excel sheet.
insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=D:\Book1.xlsx;',
'SELECT * FROM [SheetName$]') select TOP 5 CustomerID from Customers
I used the above query but it shows following error
Msg 7308, Level 16, State 1, Line 1 OLE DB provider
'Microsoft.ACE.OLEDB.12.0' cannot be used for distributed queries
because the provider is configured to run in single-threaded apartment
mode.
I have found the solution
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO
EXEC sp_MSSet_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'AllowInProcess', 1
GO
EXEC sp_MSSet_oledb_prop N'Microsoft.ACE.OLEDB.12.0', N'DynamicParameters', 1
GO
insert into OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=D:\Book1.xls;',
'SELECT * FROM [Sheet1$]') select TOP 5 CustomerID from Customers
Its working fine

How to use sp_configure in another Stored Procedure?

I am trying to use Sp_configure Proc in another stored procedure, but getting errors.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE Test01
AS
BEGIN
SET NOCOUNT ON;
sp_configure 'show advanced options', 1
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
RECONFIGURE
Go
END
GO
The Error comes:-
Msg 102, Level 15, State 1, Procedure Test01, Line 6
Incorrect syntax near 'sp_configure'.
Configuration option 'Ad Hoc Distributed Queries' changed from 1 to 1. Run the RECONFIGURE statement to install.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'END'.
GO is not a TSQL command. It is a batch separator in the query window. The first GO after RECONFIGURE effectively ends the definition of your stored procedure. You also need to use EXEC when calling the procedures. See code below.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE Test01
AS
BEGIN
SET NOCOUNT ON;
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
RECONFIGURE
END
GO

Resources