I'm trying to script sp_WhoIsActive into a table. The goal is feed the table with an Agent Job every 10 seconds.
I followed this guide and I tried to feed the table this way:
--Log activity into table.
DECLARE #destination_table VARCHAR(4000) =
'[Monitoring].[dbo].[WhoIsActive] '
EXEC sp_WhoIsActive
#get_plans = 1,
#get_transaction_info = 1,
#destination_table = #destination_table;
But as result I receive the error:
Warning: The join order has been enforced because a local join hint is used.
Msg 50000, Level 16, State 1, Procedure sp_WhoIsActive, Line 1111 [Batch Start Line 0]
Destination table not properly formatted.
On Google I found many guides talking about a solution that could help me execute a Stored Procedure into a temp table and from there I could create a table:
sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO
SELECT * INTO #MyTempTable FROM OPENROWSET('SQLNCLI', 'Server=localhost;Trusted_Connection=yes;',
'EXEC sp_WhoIsActive')
SELECT * FROM #MyTempTable
But this process too is failing with error:
Msg 11526, Level 16, State 1, Procedure sys.sp_describe_first_result_set, Line 1 [Batch Start Line 12]
The metadata could not be determined because statement 'INSERT #sessions
(
recursion,
session_id,
request_id' in procedure 'sp_WhoIsActive' uses a temp table.
I tried following Kendra Little blog but that too is not working.
In the end I scripted out the table manually:
CREATE TABLE [dbo].[WhoIsActive](
[dd_hh_mm_ss_mss] [nvarchar](50) NOT NULL,
[session_id] [tinyint] NOT NULL,
[sql_text] [nvarchar](max) NOT NULL,
[sql_command] [nvarchar](400) NOT NULL,
[login_name] [nvarchar](50) NOT NULL,
[wait_info] [nvarchar](50) NOT NULL,
[tran_log_writes] [nvarchar](50) NOT NULL,
[CPU] [smallint] NOT NULL,
[tempdb_allocations] [smallint] NOT NULL,
[tempdb_current] [smallint] NOT NULL,
[blocking_session_id] [nvarchar](50) NOT NULL,
[reads] [int] NOT NULL,
[writes] [float] NOT NULL,
[physical_reads] [tinyint] NOT NULL,
[query_plan] [nvarchar](50) NOT NULL,
[used_memory] [tinyint] NOT NULL,
[status] [nvarchar](50) NOT NULL,
[tran_start_time] [datetime2](7) NOT NULL,
[implicit_tran] [nvarchar](50) NOT NULL,
[open_tran_count] [tinyint] NOT NULL,
[percent_complete] [nvarchar](50) NOT NULL,
[host_name] [nvarchar](50) NOT NULL,
[database_name] [nvarchar](50) NOT NULL,
[program_name] [nvarchar](100) NOT NULL,
[start_time] [datetime2](7) NOT NULL,
[login_tine] [datetime2](7) NOT NULL,
[request_id] [tinyint] NOT NULL,
[collection_time] [datetime2](7) NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
But that too is failing and I cannot feed the table with a job.
sp_WhoIsActive is so popular, I cannot believe I'm the only one trying to insert the results into a table.
You need to create a table suitable to use with the output of the procedure, the schema of which can vary depending on the options you use.
SP_WhoIsActive will actually give you the create script, so to capture the default options just do
declare #definition varchar(max)
exec sp_WhoIsActive #return_schema = 1, #schema = #definition output
print #definition
This returns the appropriate T-SQL:
CREATE TABLE < table_name > (
[dd hh:mm:ss.mss] VARCHAR(8000) NULL
,[session_id] SMALLINT NOT NULL
,[sql_text] XML NULL
,[login_name] NVARCHAR(128) NOT NULL
,[wait_info] NVARCHAR(4000) NULL
,[CPU] VARCHAR(30) NULL
,[tempdb_allocations] VARCHAR(30) NULL
,[tempdb_current] VARCHAR(30) NULL
,[blocking_session_id] SMALLINT NULL
,[reads] VARCHAR(30) NULL
,[writes] VARCHAR(30) NULL
,[physical_reads] VARCHAR(30) NULL
,[used_memory] VARCHAR(30) NULL
,[status] VARCHAR(30) NOT NULL
,[open_tran_count] VARCHAR(30) NULL
,[percent_complete] VARCHAR(30) NULL
,[host_name] NVARCHAR(128) NULL
,[database_name] NVARCHAR(128) NULL
,[program_name] NVARCHAR(128) NULL
,[start_time] DATETIME NOT NULL
,[login_time] DATETIME NULL
,[request_id] INT NULL
,[collection_time] DATETIME NOT NULL
)
Edit and run with the required target table name, then you can run sp_whoisactive with the destination table option
exec sp_WhoIsActive #destination_table='Monitoring.dbo.WhoIsActive'
See the docs
I am facing a strange problem,
My c# program is inserting values into database(basically checks if the record exists then updates it, otherwise, inserts into the table)
the query for the stored procedure as well as table is shown below:
Table
CREATE TABLE [dbo].[PurchaseReceiptbySheetOil](
[id] [int] IDENTITY(1,1) NOT NULL,
[LPOId] [int] NULL,
[POLineNumber] [nvarchar](100) NULL,
[Serial] [nvarchar](100) NULL,
[DateLoaded] [date] NULL,
[truck] [nvarchar](100) NULL,
[Trailer] [nvarchar](100) NULL,
[Transporter] [nvarchar](100) NULL,
[Driver] [nvarchar](max) NULL,
[PassportNumber] [nvarchar](100) NULL,
[ObserverdVolume] [nvarchar](100) NULL,
[Terminal] [nvarchar](100) NULL,
[Tank] [nvarchar](100) NULL,
[TempInTank] [nvarchar](100) NULL,
[VCF20] [float] NULL,
[VolumeLTS20C] [float] NULL,
[Density20C] [float] NULL,
[WeightMTons] [float] NULL,
[Destination] [nvarchar](max) NULL,
[BottomSEALNRS] [nvarchar](max) NULL,
[TopSealNRS] [nvarchar](100) NULL,
[SAMPLESEALNRS] [nvarchar](max) NULL,
[PhysicalDipsOnTheTruckinMM1] [int] NULL,
[PhysicalDipsOnTheTruckinMM2] [int] NULL,
[PhysicalDipsOnTheTruckinMM3] [int] NULL,
[PhysicalDipsOnTheTruckinMM4] [int] NULL,
[PhysicalDipsOnTheTruckinMM5] [int] NULL,
[PhysicalDipsOnTheTruckinMM6] [int] NULL,
[Site] [nvarchar](100) NULL,
[LineNumber] [int] NULL,
[CreatedOn] [datetime] NULL,
[UpdatedOn] [datetime] NULL,
[CreatedBy] [nvarchar](100) NULL,
[UpdatedBy] [nvarchar](100) NULL
)
Stored Procedure
CREATE PROCEDURE [dbo].[CreateUpdatePurchaseReceiptbySheetOil]
#Site nvarchar(100),
#CreatedOn datetime,
#UpdatedOn datetime,
#CreatedBy nvarchar(100),
#UpdatedBy nVarchar(100),
#DateLoaded date,
#truck nvarchar(100),
#Trailer nvarchar(100),
#Transporter nvarchar(100),
#Driver nvarchar(max),
#PassportNumber nvarchar(100),
#ObserverdVolume nvarchar(100),
#Terminal nvarchar(100),
#Tank nvarchar(100),
#TempInTank nvarchar(100),
#VCF20 float,
#VolumeLTS20C float,
#Density20C float,
#WeightMTons float,
#Destination nvarchar(max),
#BottomSEALNRS nvarchar(max),
#SAMPLESEALNRS nvarchar(max),
#PhysicalDipsOnTheTruckinMM1 int,
#PhysicalDipsOnTheTruckinMM2 int,
#PhysicalDipsOnTheTruckinMM3 int,
#PhysicalDipsOnTheTruckinMM4 int,
#PhysicalDipsOnTheTruckinMM5 int,
#PhysicalDipsOnTheTruckinMM6 int,
#Serial Nvarchar(100),
#LineNumber int,
#POLineNumber int,
#TopSealNRS nvarchar(100),
#LPOId nvarchar(100)
AS
BEGIN
if exists(select * from [PurchaseReceiptbySheetOil] where Site= #Site and
Serial=#Serial and LineNumber = #LineNumber)
BEGIN
UPDATE [dbo].[PurchaseReceiptbySheetOil]
SET [DateLoaded] = #DateLoaded,
UpdatedOn =#UpdatedOn,
UpdatedBy=#UpdatedBy,
[truck] = #truck
,[Trailer] = #Trailer
,[Transporter] = #Transporter
,[Driver] = #Driver
,[PassportNumber] = #PassportNumber
,[ObserverdVolume] = #ObserverdVolume
,[Terminal] = #Terminal
,[Tank] = #Tank
,[TempInTank] = #TempInTank
,[VCF20] = #VCF20
,[VolumeLTS20C] = #VolumeLTS20C
,[Density20C] = #Density20C
,[WeightMTons] = #WeightMTons
,[Destination] = #Destination
,[BottomSEALNRS] = #BottomSEALNRS
,[SAMPLESEALNRS] = #SAMPLESEALNRS
,[PhysicalDipsOnTheTruckinMM1] = #PhysicalDipsOnTheTruckinMM1
,[PhysicalDipsOnTheTruckinMM2] = #PhysicalDipsOnTheTruckinMM2
,[PhysicalDipsOnTheTruckinMM3] = #PhysicalDipsOnTheTruckinMM3
,[PhysicalDipsOnTheTruckinMM4] = #PhysicalDipsOnTheTruckinMM4
,[PhysicalDipsOnTheTruckinMM5] = #PhysicalDipsOnTheTruckinMM5
,[PhysicalDipsOnTheTruckinMM6] = #PhysicalDipsOnTheTruckinMM6
,[Serial] = #Serial
,TopSealNRS=#TopSealNRS
,LPOId=#LPOId
WHERE Site= #Site and Serial=#Serial and LineNumber = #LineNumber
END
else
BEGIN
INSERT INTO [dbo].[PurchaseReceiptbySheetOil]
([LPOId]
,[POLineNumber]
,[Serial]
,[DateLoaded]
,[truck]
,[Trailer]
,[Transporter]
,[Driver]
,[PassportNumber]
,[ObserverdVolume]
,[Terminal]
,[Tank]
,[TempInTank]
,[VCF20]
,[VolumeLTS20C]
,[Density20C]
,[WeightMTons]
,[Destination]
,[BottomSEALNRS]
,[TopSealNRS]
,[SAMPLESEALNRS]
,[PhysicalDipsOnTheTruckinMM1]
,[PhysicalDipsOnTheTruckinMM2]
,[PhysicalDipsOnTheTruckinMM3]
,[PhysicalDipsOnTheTruckinMM4]
,[PhysicalDipsOnTheTruckinMM5]
,[PhysicalDipsOnTheTruckinMM6]
,[Site]
,[LineNumber]
,[CreatedOn]
,[UpdatedOn]
,[CreatedBy]
,[UpdatedBy])
VALUES
(#LPOId,#POLineNumber,
#Serial,#DateLoaded,
#truck,#Trailer,#Transporter,#Driver,
#PassportNumber,#ObserverdVolume,#Terminal,#Tank,#TempInTank,
#VCF20,#VolumeLTS20C,#Density20C,#WeightMTons,#Destination,#BottomSEALNRS,
#TopSealNRS,#SAMPLESEALNRS,
#PhysicalDipsOnTheTruckinMM1,#PhysicalDipsOnTheTruckinMM2,
#PhysicalDipsOnTheTruckinMM3,#PhysicalDipsOnTheTruckinMM4,
#PhysicalDipsOnTheTruckinMM5,#PhysicalDipsOnTheTruckinMM6,#Site,
#LineNumber,#CreatedOn,#UpdatedOn,#CreatedBy,#UpdatedBy)
END
END
Now when i run the following query to execute stored procedure:
exec sp_executesql N'EXECUTE [dbo].[CreateUpdatePurchaseReceiptbySheetOil]
#Site ,#CreatedOn ,
#UpdatedOn ,#CreatedBy
,#UpdatedBy ,#DateLoaded ,#truck
,#Trailer ,#Transporter ,#Driver
,#PassportNumber ,#ObserverdVolume
,#Terminal ,#Tank ,#TempInTank ,#VCF20
,#VolumeLTS20C ,#Density20C ,#WeightMTons
,#Destination ,#BottomSEALNRS,#TopSealNRS
,#SAMPLESEALNRS ,#PhysicalDipsOnTheTruckinMM1
,#PhysicalDipsOnTheTruckinMM2 ,#PhysicalDipsOnTheTruckinMM3
,#PhysicalDipsOnTheTruckinMM4 ,#PhysicalDipsOnTheTruckinMM5
,#PhysicalDipsOnTheTruckinMM6 ,#Serial ,#LineNumber
,#POLineNumber,#LPOId',N'#CreatedBy nvarchar(7),#CreatedOn datetime,
#UpdatedBY nvarchar(7),#UpdatedOn datetime,#Site nvarchar(7),
#DateLoaded nvarchar(9),#truck nvarchar(7),#Trailer nvarchar(7),
#Transporter nvarchar(5),#Driver nvarchar(8),#PassportNumber nvarchar(8),
#ObserverdVolume nvarchar(6),#Terminal nvarchar(5),#Tank
nvarchar(3),#TempInTank nvarchar(4),
#VCF20 nvarchar(6),#VolumeLTS20C nvarchar(6),#Density20C
nvarchar(6),#WeightMTons nvarchar(6),
#Destination nvarchar(5),#BottomSEALNRS nvarchar(14),#TopSealNRS
nvarchar(33),#SAMPLESEALNRS nvarchar(7),
#PhysicalDipsOnTheTruckinMM1 nvarchar(4),#PhysicalDipsOnTheTruckinMM2
nvarchar(4),#PhysicalDipsOnTheTruckinMM3 nvarchar(4),
#PhysicalDipsOnTheTruckinMM4 nvarchar(4),#PhysicalDipsOnTheTruckinMM5
nvarchar(4),#PhysicalDipsOnTheTruckinMM6 nvarchar(4),
#Serial nvarchar(5),#LineNumber int,#POLineNumber int,#LPOId int',
#CreatedBy=N'Tanveer',
#CreatedOn='2017-05-08 11:50:24.283',
#UpdatedBY=N'Tanveer',
#UpdatedOn='2017-05-08 11:50:24.283',
#Site=N'Site001',
#DateLoaded=N'20-Apr-17',
#truck='',
#Trailer='',
#Transporter=N'00002',
#Driver='',
#PassportNumber=''
#ObserverdVolume=N'40.000',
#Terminal=N'TOTAL',
#Tank=N'223',#TempInTank=N'30.0',#VCF20=N'0.9915',#VolumeLTS20C=N'39.660',
#Density20C=N'0.8219',
#WeightMTons=N'32.597',#Destination=N' DRC ',#BottomSEALNRS=N' 02697558/559
',#TopSealNRS=N' 02697560 TO 562+02697564 TO 566 ',
#SAMPLESEALNRS=N'781845 '
,#PhysicalDipsOnTheTruckinMM1=N'1472',#PhysicalDipsOnTheTruckinMM2=N'1592',
#PhysicalDipsOnTheTruckinMM3=N'1706',#PhysicalDipsOnTheTruckinMM4=N'1832',
#PhysicalDipsOnTheTruckinMM5=N'1808',
#PhysicalDipsOnTheTruckinMM6=N'1362',#Serial=N'00001',#LineNumber=8,
#POLineNumber=0,#LPOId=7
the values for the columns get swapped during the insert, Note the value for serial is 0001 but it will insert as 1362 instead of 0001 and for some other columns also the values will change(not all).
Please note that I have checked the sequence of columns in insert and
values sections. I am specifying columns but still facing this issue.
I have dropped and created the table again.
During debugging in sql
server, the value is correct when debugging starts i.e.0001 but then
changes, i don\t know why.
The sequence of parameters in Stored procedure definition should be the same when we are executing it, I was assuming it is not depending on the sequence but the parameter name and i was wrong.
I have 2 tables,each contains 4-500k records
CREATE TABLE [dbo].[User][UserId] [int] IDENTITY(1,1) NOT NULL,
[Password] [nvarchar](max) NULL,
[RoleId] [int] NOT NULL,
[Name] [nvarchar](max) NULL,
[Address] [nvarchar](max) NULL,
[Email] [nvarchar](max) NULL,
[Landline] [nvarchar](max) NULL,
[MobileNumberCode] [int] NULL,
[MobileNumber] [nvarchar](max) NULL,
[DateOfBirth] [datetime] NULL,
[MarriageDate] [datetime] NULL,
[CreatedDate] [datetime] NOT NULL,
[UpdatedDate] [datetime] NOT NULL,
[Status] [nvarchar](max) NOT NULL,
[BranchId] [int] NULL,
[UserTitle] [nvarchar](50) NULL,
[MiddleName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[HouseNumber] [nvarchar](50) NULL,
[BuildingNumber] [nvarchar](50) NULL,
[RoadNumber] [nvarchar](50) NULL,
[BlockNumber] [nvarchar](50) NULL,
[City] [nvarchar](50) NULL,
[NearBranchId] [int] NULL,
[MobileIsValid] [bit] NULL,
[EmailIsValid] [bit] NULL,
[Gender] [nvarchar](50) NULL,
[SourceId] [int] NULL)
CREATE TABLE [dbo].[PurchaseOrder]
[PurchaseOrderId] [int] NOT NULL,
[BranchId] [int] NOT NULL,
[PurchaseDate] [datetime] NOT NULL,
[Amount] [decimal](18, 3) NOT NULL,
[UserId] [int] NOT NULL,
[Status] [nvarchar](max) NULL,
[sbs_no] [int] NOT NULL)
And I have stored procedure to get data from these tables using join.
CREATE PROC Sp_SearchCustomer (#FromDate datetime = null,
#ToDate datetime = null,
#RegFromDate datetime = null,
#RegToDate datetime = null)
AS
BEGIN
select a.UserId,a.Name,b.PurchaseOrderId,b.Amount from dbo.[User] a left join PurchaseOrder b on a.UserId=b.UserId
where
((a.CreatedDate >= ''' + cast(#RegFromDate as varchar) + ''')
AND (a.CreatedDate <= ''' + cast(#RegToDate as varchar) + '''))
and ((b.PurchaseDate >= ''' + cast(#FromDate as varchar) + ''')
AND (b.PurchaseDate <= ''' + cast(#ToDate as varchar) + '''))
END
When executing this procedure with date, its getting "The wait operation timed out" exception. Please help to solve this issue.
Your date in your tables and in your Procedure are both saved as varchar. This is perfect and there is no need to convert them to varchar.
Beside, varchar is surrounded by quotes and won't be executed. This is just becoming a string:
where ((a.CreatedDate >= 'cast(#RegFromDate as varchar)')...
There are also way too many useless parenthesis since you are using AND.
Try this instead:
CREATE PROC Sp_SearchCustomer (
#FromDate datetime = null,
#ToDate datetime = null,
#RegFromDate datetime = null,
#RegToDate datetime = null
)
AS
BEGIN
SELECT a.UserId
,a.Name
,b.PurchaseOrderId
,b.Amount
FROM dbo.[User] a
LEFT JOIN PurchaseOrder b
ON a.UserId = b.UserId
WHERE
a.CreatedDate >= #RegFromDate
AND a.CreatedDate <= #RegToDate
AND b.PurchaseDate >= #FromDate
AND b.PurchaseDate <= #ToDate
END
Once the query has been improved, you can test it again.
You should also look at Statistics and Indexes and make sure that Statistics are up-to-date and Indexes are not fragmented.
For Statistics, you can use: exec sp_updatestats
For Indexes on these 2 tables, look at the Fragmentation % and choose to REBUILD or REORGANIZE them.
When you have 2 tables table like this
CREATE TABLE #BranchType
(
[External_BranchTypeID] [uniqueidentifier] DEFAULT newsequentialid() NOT NULL,
[BranchTypeID] [smallint] identity(1,1) ,
[BranchTypeDescription] [nvarchar](20) NOT NULL,
[DateCreated] [datetime] NOT NULL,
[UserCreated] [nvarchar](20) NOT NULL,
[DateModified] [datetime] NULL,
[UserModified] [nvarchar](20) NULL,
[IsDeleted] [bit] NOT NULL,
)
CREATE TABLE BranchSubType
(
[External_BranchSubTypeID] [uniqueidentifier] DEFAULT newsequentialid() NOT NULL,
[BranchSubTypeID] [smallint] identity(1,1) ,
[BranchTypeID] [uniqueidentifier] NOT NULL,
[BranchSubTypeDescription] [nvarchar](30) NOT NULL,
[FinancialSystemTypeId] [smallint] NOT NULL,
[DateCreated] [datetime] NOT NULL,
[UserCreated] [nvarchar](20) NOT NULL,
[DateModified] [datetime] NULL,
[UserModified] [nvarchar](20) NULL,
[IsDeleted] [bit] NOT NULL,
)
How can you do an insert like the one below in SQL Server? I am trying to return the guid value
DECLARE #SQLCmd VARCHAR(max)
set #SQLCmd = 'SELECT External_BranchTypeID FROM #BranchType WHERE BranchTypeID =1'
INSERT INTO BranchSubType (BranchTypeID, BranchSubTypeDescription, BranchSubTypeId, DateCreated, UserCreated,IsDeleted)
VALUES ( exec(#SQLCmd), 'Normal',1, getdate(), 'System',0) --FROM #BranchType A WHERE A.BranchTypeID = 1
In this case you don't need to use EXEC
INSERT INTO BranchSubType
(BranchTypeID,
BranchSubTypeDescription,
BranchSubTypeId,
DateCreated,
UserCreated,
IsDeleted)
SELECT External_BranchTypeID,
'Normal',
1,
getdate(),
'System',
0
FROM #BranchType WHERE BranchTypeID =1
I have a database that can have data updated from two external parties.
Each of those parties sends a pipe delimited text file that is BULK INSERTED into the staging table.
I now want to change the scheme for one of the parties by adding a few columns, but this is unfortunately breaking the BULK INSERT for the other party even though the new columns are all added as NULLABLE.
Is there any obvious solution to this?
TABLE SCHEMA:
CREATE TABLE [dbo].[CUSTOMER_ENTRY_LOAD](
[CARD_NUMBER] [varchar](12) NULL,
[TITLE] [varchar](6) NULL,
[LAST_NAME] [varchar](34) NULL,
[FIRST_NAME] [varchar](40) NULL,
[MIDDLE_NAME] [varchar](40) NULL,
[NAME_ON_CARD] [varchar](26) NULL,
[H_ADDRESS_PREFIX] [varchar](50) NULL,
[H_FLAT_NUMBER] [varchar](5) NULL,
[H_STREET_NUMBER] [varchar](10) NULL,
[H_STREET_NUMBER_SUFFIX] [varchar](5) NULL,
[H_STREET] [varchar](50) NULL,
[H_SUBURB] [varchar](50) NULL,
[H_CITY] [varchar](50) NULL,
[H_POSTCODE] [varchar](4) NULL,
[P_ADDRESS_PREFIX] [varchar](50) NULL,
[P_FLAT_NUMBER] [varchar](5) NULL,
[P_STREET_NUMBER] [varchar](10) NULL,
[P_STREET_NUMBER_SUFFIX] [varchar](5) NULL,
[P_STREET] [varchar](50) NULL,
[P_SUBURB] [varchar](50) NULL,
[P_CITY] [varchar](50) NULL,
[P_POSTCODE] [varchar](4) NULL,
[H_STD] [varchar](3) NULL,
[H_PHONE] [varchar](7) NULL,
[C_STD] [varchar](3) NULL,
[C_PHONE] [varchar](10) NULL,
[W_STD] [varchar](3) NULL,
[W_PHONE] [varchar](7) NULL,
[W_EXTN] [varchar](5) NULL,
[DOB] [smalldatetime] NULL,
[EMAIL] [varchar](50) NULL,
[DNS_STATUS] [bit] NULL,
[DNS_EMAIL] [bit] NULL,
[CREDITCARD] [char](1) NULL,
[PRIMVISACUSTID] [int] NULL,
[PREFERREDNAME] [varchar](100) NULL,
[STAFF_NUMBER] [varchar](50) NULL,
[CUSTOMER_ID] [int] NULL,
[IS_ADDRESS_VALIDATED] [varchar](50) NULL
) ON [PRIMARY]
BULK INSERT STATEMENT:
SET #string_temp = 'BULK INSERT customer_entry_load FROM '+char(39)+#inpath
+#current_file+'.txt'+char(39)+' WITH (FIELDTERMINATOR = '+char(39)+'|'+char(39)
+', MAXERRORS=1000, ROWTERMINATOR = '+char(39)+'\n'+char(39)+')'
SET DATEFORMAT dmy
EXEC(#string_temp)
The documentation describes how to use a format file to handle the scenario where the target table has more columns than the source file. An alternative that can sometimes be easier is to create a view on the table and BULK INSERT into the view instead of the table; this possibility is described in the same documentation.
And please always mention your SQL Server version.
Using OPENROWSET with BULK allows you to use your file in a query. You can use that to format the data and select only the columns you need.
In the end I have handled the two different cases with two different BULK INSERT statements (depending on which file is being processed). It seems like there isn't a way to do what I was trying to do with one statement.
You could use the format file idea supplied by #Pondlife.
Adapt your insert dynamically based on the input file name (provided there are unique differneces between the external parties). Using a CASE statement, simply select the correct format file based on the unique identifier in the file name.
DECLARE #formatFile varchar (max);
Set #formatFile =
CASE
WHEN #current_file LIKE '%uniqueIdentifier%'
THEN 'file1'
ELSE 'file2'
END
SET #string_temp = 'BULK INSERT customer_entry_load FROM '+char(39)+#inpath
+#current_file+'.txt'+char(39)+' WITH (FORMATFILE = '+char(39)+#formatFile+char(39)
')'
SET DATEFORMAT dmy
EXEC(#string_temp)
Hope that helps!