Exporting to txt file not allowing more than 255 characters - sql-server

Below is the script that i have used which shows that i have used BCP process and some shell script to generate the extracts. we need to take the data from sql table and dump as it is in table to flat file. So these can be used to load in hive tables and qlick apps for users to have a look at the data.
ALTER Procedure [dbo].[GenerateEAPExtracts] #tblName NVARCHAR(MAX), #filePath VARCHAR(255)
As
BEGIN
IF OBJECT_ID('tempdb..#Temp1') IS NOT NULL
BEGIN
Drop table #Temp1
END
--DECLARE #tblName NVARCHAR(MAX) = 'BMI.BMI_Invites'
DECLARE #SchemaName NVARCHAR(50)
DECLARE #tbl varchar(100)
DECLARE #DBName varchar(100)
SET #tbl= Replace(#tblname,(Left(#tblname,(LEN(#tblname)-(charindex('.',reverse(#tblname))-1)))),'');
SET #SchemaName= SUBSTRING(#tblName,1,LEN(Left(#tblname,(LEN(#tblname)-(charindex('.',reverse(#tblname))-1))))-1);
DECLARE #SQL NVARCHAR(MAX)=''
DECLARE #SQL1 NVARCHAR(MAX)=''
DECLARE #ColConvert NVARCHAR(MAX)=''
DECLARE #ColName NVARCHAR(MAX)=''
DECLARE #ColName1 NVARCHAR(MAX)=''
DECLARE #ColName2 NVARCHAR(MAX)=''
DECLARE #Dt NVARCHAR(MAX)=''
DECLARE #I INT=1, #COUNT INT;
Create table #Temp1(
ID INT IDENTITY(1,1),
[DBName] [nvarchar](255) NULL,
[TableName] [nvarchar](255) NULL,
[Attributes] [nvarchar](MAX) NULL,
[DataType] [nvarchar](MAX) NULL,
[TceMdf_Dt] [date] NULL
)
SET #SQL = 'INSERT INTO #Temp1 select TABLE_CATALOG as [DBName],
Table_Name as [TableName],
Column_name as [Attributes],
DATA_TYPE as [DataType],
getdate() as [TceMdf_Dt]
from Information_schema.columns where Table_Schema ='''+ #SchemaName +''' AND table_name = '''+#tbl+''''
print #SQL
--EXEC SP_EXECUTESQL #SQL
EXEC(#SQL)
SELECT #SQL1='Select #totCnt= count(1) FROM Information_schema.columns where Table_Schema ='''+ #SchemaName +''' AND table_name = '''+#tbl+''''
print #SQL1
EXECUTE sp_executesql #SQL1, N'#totCnt int OUTPUT', #totCnt=#COUNT OUTPUT
WHILE #I <= #COUNT
BEGIN
SELECT #ColName = Attributes FROM #Temp1 WHERE ID = #I AND [TableName]=#tbl;
SELECT #DBName = DBName FROM #Temp1 WHERE ID = #I AND [TableName]=#tbl;
SELECT #Dt= DataType FROM #Temp1 WHERE ID = #I AND [TableName]=#tbl AND [Attributes]=#ColName
IF #Dt ='Date' or #Dt='Datetime' or #Dt='Decimal' or #Dt='Int' or #Dt='Float' or #Dt='datetime2' or #Dt='Numeric' or #Dt='Real' or #Dt='bit' or #Dt='tinyint'
BEGIN
SET #ColConvert='convert(varchar(25),['+#ColName+'],120)'
--EXECUTE sp_executesql #ColConvert
SET #ColName2 = #ColName2 + ',' + #ColConvert
--EXECUTE sp_executesql #ColName2
END
ELSE
BEGIN
SET #ColName2 = #ColName2 + ',' + '['+#ColName+']'
--EXECUTE sp_executesql #ColName2
END
SET #ColName1 = #ColName1 +''''+''''+','+''''+''''+ #ColName
--EXECUTE sp_executesql #ColName1
SET #I=#I+1;
END
SET #ColName1=RIGHT(#ColName1, LEN(#ColName1) - 3) + ''''''
SET #ColName2=LTRIM(RTRIM(RIGHT(#ColName2, LEN(#ColName2) - 1)))
--print #ColName1
--print #ColName2
--DECLARE #filePath VARCHAR(255)
DECLARE #fileName VARCHAR(255)
DECLARE #sqlCommand VARCHAR(MAX)
DECLARE #sqlCommand1 VARCHAR(MAX)
DECLARE #sqlCommand2 VARCHAR(MAX)
DECLARE #qry nvarchar(max)='(SELECT MaxCDate from '+#DBName+'.config.EAP_Extracts where TableName= '''''+ #tbl +''''')'
print #qry
--exec(#qry)
--SET #filePath = 'C:\'
SET #fileName = #tbl + '_'
+ CONVERT(VARCHAR, GETDATE(), 112) + '.csv'
set #sqlCommand = 'SQLCMD.EXE -S LocalHost -d TCE_WW -E -h -1 -s"|" -W -Q "set nocount on;SELECT ' +#ColName1+ ' ; Select ' +#ColName2+ ' FROM '+#DBName+'.' +#tblName+ ''
print(#sqlCommand)
set #sqlCommand1 =' where TceMdf_Dt > '+ #qry +''
set #sqlCommand2 ='" -o "'+#filePath+#fileName+'" -f 65001'
--declare #chkQry varchar(max) = #sqlCommand+#sqlCommand1+#sqlCommand2
--print(#chkQry)
Update config.EAP_Extracts Set [SqlMaxQuery]=#sqlCommand+#sqlCommand1+#sqlCommand2 where TableName=''+#tbl+''
declare #CommandString varchar(max)= (select distinct 'EXEC xp_cmdshell '+''''+[SqlMaxQuery]+'''' from config.EAP_Extracts where TableName=''+#tbl+'')
--declare #CommandString varchar(max)= 'EXEC xp_cmdshell ' + ''''+#chkQry+''''
print #CommandString
EXEC (#CommandString)
declare #SQLQuery varchar(max)='Update '+#DBName+'.config.EAP_Extracts Set MaxCDate=(Select Max(TceMdf_Dt) from '+ #tblName+') where TableName='''+#tbl+''''
--EXEC xp_cmdshell 'bcp "SELECT ''update_action'',''UserName'',''lastupdate'',''Quote_Num'',''Region'',''ExternalDataReference'',''TceLd_Dt'',''TceMdf_Dt'',''Record_Status'' ; Select LTRIM(RTRIM[update_action]),LTRIM(RTRIM[UserName]),LTRIM(RTRIM[lastupdate]),LTRIM(RTRIM[Quote_Num]),LTRIM(RTRIM[Region]),LTRIM(RTRIM[ExternalDataReference]),convert(varchar(25),LTRIM(RTRIM[TceLd_Dt]),120),convert(varchar(25),LTRIM(RTRIM[TceMdf_Dt]),120),LTRIM(RTRIM[Record_Status]) FROM TCE_WW.BMI.BMI_Invites where TceMdf_Dt > (SELECT MaxCDate from TCE_WW.config.EAP_Extracts where TableName= 'BMI_Invites')" queryout "C:\BMI_Invites_20190722.txt" -T -c -q -y 0 -t"|"'
exec (#SQLQuery)
print(#SQLQuery)
Drop table #Temp1
END

Related

Create dynamic stored procedure for all databases with specific table

I would like to add the following stored procedure to all existing databases which contain the table schichten. All my approaches have failed so I'm looking for help here.
This is my approach:
IF object_id('tempdb.dbo.#database') is not null
drop TABLE #database
GO
CREATE TABLE #database(id INT identity primary key, name sysname)
GO
SET NOCOUNT ON
INSERT INTO #database(name)
SELECT name
FROM sys.databases
WHERE source_database_id is null
ORDER BY name
SELECT * FROM #database
DECLARE #id INT, #cnt INT, #sql NVARCHAR(MAX), #currentDb SYSNAME;
SELECT #id = 1, #cnt = max(id) FROM #database
WHILE #id <= #cnt
BEGIN
BEGIN TRY
SELECT #currentDb = name
FROM #database
WHERE id = #id
IF OBJECT_ID(#currentDb+'.dbo.schichten') IS NOT NULL
CREATE PROCEDURE #currentDb.[dbo].[Ausw_Tabelle_Taxi_Pers_Jahr]
#ColumnName nvarchar(MAX),
#Selector nvarchar(MAX),
#Gesamtergebnis nvarchar(MAX)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql1 AS NVARCHAR(MAX),
#ASSelector nvarchar(MAX),
#IFPers nvarchar(MAX);
IF #Selector = 'konz'
BEGIN
SET #ASSelector = 'Taxi'
SET #IFPers=''
END
ELSE
BEGIN
SET #ASSelector = 'Personal'
SET #IFPers = '[name] AS Name,'
END
SET #sql1 = N';WITH temp AS (SELECT *
FROM (
SELECT
ISNULL((DATENAME(m,[datum])+ cast(datepart(yyyy,[datum]) as varchar(5))),0) AS MONTHYEAR,
ISNULL(['+ #Selector +'],0) AS '+ #ASSelector +','+ #IFPers +'
ISNULL((ISNULL([umsum],0) +
ISNULL([sonst_0],0) +
ISNULL([sonst_7],0) +
ISNULL([sonst_16],0) +
ISNULL([sonst_z],0) -
ISNULL([ff],0)),0) AS UMSATZSUMME
FROM [dbo].[schichten]
) AS SOURCE
PIVOT (SUM([UMSATZSUMME]) FOR [MONTHYEAR] IN ('+ #ColumnName + N' )) AS UMSAETZE )
SELECT *, '+ #Gesamtergebnis +' AS Gesamtergebnis FROM temp ORDER BY '+ #ASSelector +''
EXEC sp_executesql #sql
END
END TRY
BEGIN CATCH
END CATCH
SET #id = #id + 1;
END
GO
I am hoping that there is someone who can help me.
You have to execute the create procedure separately, so if you wrap it up into a variable and use exec sp_executesql it should work.
IF object_id('tempdb.dbo.#database') is not null
drop TABLE #database
GO
CREATE TABLE #database(id INT identity primary key, name sysname)
GO
SET NOCOUNT ON
INSERT INTO #database(name)
SELECT name
FROM sys.databases
WHERE source_database_id is null
ORDER BY name
SELECT * FROM #database
DECLARE #id INT, #cnt INT, #sql NVARCHAR(MAX), #currentDb SYSNAME;
SELECT #id = 1, #cnt = max(id) FROM #database
WHILE #id <= #cnt
BEGIN
BEGIN TRY
SELECT #currentDb = name
FROM #database
WHERE id = #id
IF OBJECT_ID(#currentDb+'.dbo.schichten') IS NOT NULL
begin
set #sql = 'CREATE PROCEDURE '+#currentDb+'.[dbo].[Ausw_Tabelle_Taxi_Pers_Jahr]
#ColumnName nvarchar(MAX),
#Selector nvarchar(MAX),
#Gesamtergebnis nvarchar(MAX)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql1 AS NVARCHAR(MAX),
#ASSelector nvarchar(MAX),
#IFPers nvarchar(MAX);
IF #Selector = ''konz''
BEGIN
SET #ASSelector = ''Taxi''
SET #IFPers=''''
END
ELSE
BEGIN
SET #ASSelector = ''Personal''
SET #IFPers = ''[name] AS Name,''
END
SET #sql1 = N'';WITH temp AS (SELECT *
FROM (
SELECT
ISNULL((DATENAME(m,[datum])+ cast(datepart(yyyy,[datum]) as varchar(5))),0) AS MONTHYEAR,
ISNULL([''+ #Selector +''],0) AS ''+ #ASSelector +'',''+ #IFPers +''
ISNULL((ISNULL([umsum],0) +
ISNULL([sonst_0],0) +
ISNULL([sonst_7],0) +
ISNULL([sonst_16],0) +
ISNULL([sonst_z],0) -
ISNULL([ff],0)),0) AS UMSATZSUMME
FROM [dbo].[schichten]
) AS SOURCE
PIVOT (SUM([UMSATZSUMME]) FOR [MONTHYEAR] IN (''+ #ColumnName + N'' )) AS UMSAETZE )
SELECT *, ''+ #Gesamtergebnis +'' AS Gesamtergebnis FROM temp ORDER BY ''+ #ASSelector +''''
EXEC sp_executesql #sql1
END'
EXEC sp_executesql #sql
END TRY
BEGIN CATCH
END CATCH
SET #id = #id + 1;
END
GO
Assuming this is a one time need as opposed to a nightly maintenance task, you can use a built-in stored procedure, sys.sp_MSforeachdb, to execute a statement in each database. It is safe to use and has been discussed extensively on the web. However, it is an undocumented feature and can be removed without notice by Microsoft so you don't want to depend on it for recurring tasks.
Create and validate your statement in one database, then use this stored procedure to execute it in all of the databases. ? is the placeholder for the database name.
EXEC sys.sp_MSforeachdb #command1 =
'IF OBJECT_ID(''?.dbo.schichten'') IS NOT NULL
AND OBJECT_id(''?.[dbo].[Ausw_Tabelle_Taxi_Pers_Jahr]'') IS NOT NULL
BEGIN
CREATE PROCEDURE ?.[dbo].[Ausw_Tabelle_Taxi_Pers_Jahr]
#ColumnName nvarchar(MAX),
#Selector nvarchar(MAX),
#Gesamtergebnis nvarchar(MAX)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql1 AS NVARCHAR(MAX),
#ASSelector nvarchar(MAX),
#IFPers nvarchar(MAX);
IF #Selector = ''konz''
BEGIN
SET #ASSelector = ''Taxi''
SET #IFPers=''''
END
ELSE
BEGIN
SET #ASSelector = ''Personal''
SET #IFPers = ''[name] AS Name,''
END
SET #sql1 = N'';WITH temp AS (SELECT *
FROM (
SELECT
ISNULL((DATENAME(m,[datum])+ cast(datepart(yyyy,[datum]) as varchar(5))),0) AS MONTHYEAR,
ISNULL([''+ #Selector +''],0) AS ''+ #ASSelector +'',''+ #IFPers +''
ISNULL((ISNULL([umsum],0) +
ISNULL([sonst_0],0) +
ISNULL([sonst_7],0) +
ISNULL([sonst_16],0) +
ISNULL([sonst_z],0) -
ISNULL([ff],0)),0) AS UMSATZSUMME
FROM [dbo].[schichten]
) AS SOURCE
PIVOT (SUM([UMSATZSUMME]) FOR [MONTHYEAR] IN (''+ #ColumnName + N'' )) AS UMSAETZE )
SELECT *, ''+ #Gesamtergebnis +'' AS Gesamtergebnis FROM temp ORDER BY ''+ #ASSelector +''''
EXEC sp_executesql #sql1
END
'

BCP Date Formatting

I'm converting a date to a string in a Stored Procedure, to send to a BCP. This works but the formatting in the CSV appears to open as dd-mm-yyyy despite using type 103 which should show dd/mm/yyyy' (see https://msdn.microsoft.com/en-us/library/ms187928.aspx)
Code:
USE [Database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spExcelIntoTableMainReport]
(
#tableName sysname,
#xModuleID varchar(Max)
)
AS
BEGIN
---------------------------------------
DECLARE #FilePath varchar(150)
SELECT #FilePath = 'C:\Sites\Reposts\'
--------------------------------------------------------------
DECLARE #sql NVARCHAR(MAX)
---------------------------------------
DECLARE #tableCREATE NVARCHAR(2500)
SET #tableCREATE = 'CREATE TABLE ' + #tableName + '
(
[ID] VARCHAR(200),
DateFrom varchar(100),
DateTo varchar(100)
)'
EXEC (#tableCREATE)
---------------------------------------
SET #sql = '
INSERT INTO ' + #tableName + '
(
ID,
DateFrom ,
DateTo
)
VALUES
(
''ID'',
''Date From (DD/MM/YYYY)'' ,
''Date to (DD/MM/YYYY)''
)'
EXEC (#sql)
SET #sql = '
INSERT INTO ' + #tableName + '
(
ID,
DateFrom ,
DateTo
)
SELECT ID,
LTRIM(RTRIM(CONVERT(varchar(10),cast(ModuleDateStart as Date),103))),
LTRIM(RTRIM(CONVERT(varchar(10),cast(ModuleDateComplete as Date),103) )),
FROM TBLRESULTS'
EXEC (#sql)
--------- Now we have a table create the CSV ----------
DECLARE #sql1 varchar(8000)
SELECT #sql1 = 'bcp DATABASE+ #tableName + ' out ' + #FilePath + ''+ #tableName + '.csv -c -t, -T -S '+ ##servername
EXEC master..xp_cmdshell #sql1
END

Returning Data from a SQL Server stored procedure

I would like to ask how am I going to return the count(*) because every time I call the stored procedure, it just prints the result.
Here is the code :
ALTER PROCEDURE sp_returnCount
#tblname sysname
, #colname sysname
, #key varchar(10)
AS
DECLARE #sql nvarchar(4000)
DECLARE #num INT
DECLARE #params NVARCHAR (4000)
SELECT #sql = 'SELECT COUNT(*) ' +
' FROM dbo.' + quotename(#tblname) +
' WHERE ' + quotename(#colname) + ' LIKE #key'
EXEC sp_executesql #sql, N'#key varchar(10)', #key
--just prints 5 or any numbers...
I'd like to return the count(*) to use it in another query. Thanks in advance.
ALTER PROCEDURE sp_returnCount
#tblname sysname
, #colname sysname
, #key varchar(10)
AS
DECLARE #sql nvarchar(4000)
DECLARE #num INT
DECLARE #params NVARCHAR (4000)
DECLARE #count int
SELECT #sql = 'SELECT #count = COUNT(*) ' +
' FROM dbo.' + quotename(#tblname) +
' WHERE ' + quotename(#colname) + ' LIKE #key'
EXEC sp_executesql #sql, N'#key varchar(10), #count int OUTPUT', #key, #count OUTPUT

Stored Procedure if Exist with dynamically table

I have a problem here, I'm searching in stackoverflow but still not find the best way
i have a stored procedure (SP) like this
DECLARE #table NVARCHAR(max), #SQLQuery NVARCHAR(max)
SET #table = #meta+'_prize4winner'
SET #SQLQuery = 'if exists (Select * from [dbo].' + #table + ' where idCampaignLog =''' + convert(nvarchar, #ID) +''') exec InsertC2CWinner''' + convert(nvarchar, #meta) +''','''+ convert(nvarchar, #ID)+''',null else select ''you lose ''as winStatus'
execute SP_EXECUTESQL #SQLQuery
need help how and where to set the output if I want the output if exist 'YOU WIN' else 'You Lose' thx
The general syntax is like this
DECLARE #retval int
DECLARE #sSQL nvarchar(500);
DECLARE #ParmDefinition nvarchar(500);
DECLARE #tablename nvarchar(50)
SELECT #tablename = N'products'
SELECT #sSQL = N'SELECT #retvalOUT = MAX(ID) FROM ' + #tablename;
SET #ParmDefinition = N'#retvalOUT int OUTPUT';
EXEC sp_executesql #sSQL, #ParmDefinition, #retvalOUT=#retval OUTPUT;
SELECT #retval;

Return true if view consist multiple table else false

I need return result in terms of true or false from Stored Procedure by detecting a view contains multiple tables or not.
Attempt:
CREATE PROC spTest
#ViewName nvarchar(max)
AS
DECLARE #SQL nvarchar(max)
DECLARE #TableName nvarchar(max)
SET #SQL = 'SELECT #TableName = Table_Name
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE View_Name = ''' + #ViewName + ''''
EXEC sp_executesql #SQL, N'#TableName nvarchar(max) OUTPUT', #TableName output
IF (#TableName > 1)
BEGIN
SELECT 'True'
END
ELSE
BEGIN
SELECT 'False'
END
GO
Note: I am not getting how to insert all tables from view into #TableName variable and check the condition.
you can achive your goal through this
create PROC spTest
#ViewName nvarchar(max)
AS
DECLARE #SQL nvarchar(max)
SET #SQL = '
DECLARE #TableName table (table_name varchar(1000))
insert into #TableName
SELECT Table_Name
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE View_Name = ''' + #ViewName + '''
IF (select count(1) from #TableName )> 1
BEGIN
SELECT ''True''
END
ELSE
BEGIN
SELECT ''False''
END'
exec (#SQL)
GO
After creating procedure execute procedure
spTest 'pace_entity_access_view'
CREATE PROC Sptest #ViewName NVARCHAR(max)
AS
DECLARE #cnt INT
SELECT #cnt = Count(*)
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE VIEW_NAME = #ViewName
IF ( #cnt > 1 )
BEGIN
SELECT 'True'
END
ELSE
BEGIN
SELECT 'False'
END

Resources