Showing posts with label bulk. Show all posts
Showing posts with label bulk. Show all posts

Friday, March 9, 2012

Error Msg 141 on Bulk Insert

I've got the following SP to automatically insert all files in a directory into the database:

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE PROCEDURE Imp_Header_PO_sp

@.FilePath varchar(1000) = 'D:\EBT\Outbound\',

@.WIPPath varchar(1000) = 'D:\EBT\Outbound\WIP',

@.ArchivePath varchar(1000) = 'D:\EBT\Outbound\Archive',

@.FileNameMask varchar(1000) = '*Header.txt'

AS

BEGIN

SET NOCOUNT ON;

declare @.Filename varchar(1000),

@.File varchar(1000)

declare @.cmd varchar(2000)

create table #Dir (s varchar(8000))

-- Move Header files to WIP

select @.cmd = 'move ' + @.FilePath + @.FileNameMask + ' ' + @.WIPPath

select @.cmd = 'dir /B ' + @.WIPPath + @.FileNameMask

delete #Dir

insert #Dir exec master..xp_cmdshell @.cmd

delete #Dir where s is null or s like '%not found%'

-- Import file

while exists (select * from #Dir)

begin

select @.FileName = min(s) from #Dir

select @.File = @.WIPPath + @.FileName

select @.cmd = 'bulk insert'

select @.cmd = @.cmd + ' POWebOutHeader'

select @.cmd = @.cmd + ' from'

select @.cmd = @.cmd + ' ''' + replace(@.File,'"','') + ''''

select @.cmd = @.cmd + ' with (Fieldterminator = ',')'

-- Import the data

exec (@.cmd)

-- remove filename just imported

delete #Dir where s = @.FileName

-- Archive the file

select @.cmd = 'move ' + @.WIPPath + @.FileName + ' ' + @.ArchivePath + @.FileName

exec master..xp_cmdshell @.cmd

end

drop table #Dir

END

GO

When I try to execute the code, I get the following error, on this line: select @.cmd = @.cmd + ' with (Fieldterminator = ',')'

Msg 141, Level 15, State 1, Procedure Imp_Header_PO_sp, Line 46

A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.

I've tried to find a fix for this error, but it seams to only relate to a select statement and not a Bulk Insert. Can someone please help me figure out how to fix this error?

Thanks,

Laura

Causes:

This error occurs when you are assigning the column values from a SELECT statement into local variables but not all columns are assigned to a corresponding local variable.

|||

The error occurs because you need to double your quotes

select @.cmd = @.cmd + ' with (Fieldterminator = '','')'

instead of

select @.cmd = @.cmd + ' with (Fieldterminator = ',')'

Run these 2 example to see the error

declare @.cmd varchar(50)
select @.cmd =''
select @.cmd = @.cmd + ' with (Fieldterminator = ',')'
print @.cmd

declare @.cmd varchar(50)
select @.cmd =''
select @.cmd = @.cmd + ' with (Fieldterminator = '','')'
print @.cmd

Denis the SQL Menace

http://sqlservercode.blogspot.com/

|||You are awesome! Thanks!

Friday, February 24, 2012

error message on backup

When trying to do a backup, I come up with this message.
Backup, CHECKALLOC, bulk copy, SELECT INTO, and file
manipulation (such as CREATE FILE) operations on a
database must be serialized. Reissue the statement after
the current backup, CHECKALLOC, or file manipulation
operation is completed. [SQLSTATE 42000] (Error 3023)
Backup or restore operation terminating abnormally.
[SQLSTATE 42000] (Error 3013). The step failed.
What could be causing this failed job?Read the error message closely. You are trying to execute two operations at
the same time, and those operations aren't allowed to execute at the same
time. Check your job schedules etc.
--
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"SQL'''?" <anonymous@.discussions.microsoft.com> wrote in
message news:368b01c3aa26$9a6deb80$a601280a@.phx.gbl...
> When trying to do a backup, I come up with this message.
> Backup, CHECKALLOC, bulk copy, SELECT INTO, and file
> manipulation (such as CREATE FILE) operations on a
> database must be serialized. Reissue the statement after
> the current backup, CHECKALLOC, or file manipulation
> operation is completed. [SQLSTATE 42000] (Error 3023)
> Backup or restore operation terminating abnormally.
> [SQLSTATE 42000] (Error 3013). The step failed.
> What could be causing this failed job?|||Only one job is running during the schedule. I even
killed every job right now and tried to run the backup,
and it came up with the same message.
>--Original Message--
>Read the error message closely. You are trying to execute
two operations at
>the same time, and those operations aren't allowed to
execute at the same
>time. Check your job schedules etc.
>--
>Tibor Karaszi, SQL Server MVP
>Archive at:
>http://groups.google.com/groups?
oi=djq&as_ugroup=microsoft.public.sqlserver
>
>"SQL'''?"
<anonymous@.discussions.microsoft.com> wrote in
>message news:368b01c3aa26$9a6deb80$a601280a@.phx.gbl...
>> When trying to do a backup, I come up with this message.
>> Backup, CHECKALLOC, bulk copy, SELECT INTO, and file
>> manipulation (such as CREATE FILE) operations on a
>> database must be serialized. Reissue the statement after
>> the current backup, CHECKALLOC, or file manipulation
>> operation is completed. [SQLSTATE 42000] (Error 3023)
>> Backup or restore operation terminating abnormally.
>> [SQLSTATE 42000] (Error 3013). The step failed.
>> What could be causing this failed job?
>
>.
>|||Could be an autogrow or autoshrink. You might have to use Profiler and/or
Performance Monitor to determine where the conflict is. If you can hunt it
down, I suggest you open a case with MS (I assume you have searched KB). One
cannot rule out the possibility of some bug in SQL Server.
--
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
<anonymous@.discussions.microsoft.com> wrote in message
news:02e701c3aa2c$eae205f0$a401280a@.phx.gbl...
> Only one job is running during the schedule. I even
> killed every job right now and tried to run the backup,
> and it came up with the same message.
>
> >--Original Message--
> >Read the error message closely. You are trying to execute
> two operations at
> >the same time, and those operations aren't allowed to
> execute at the same
> >time. Check your job schedules etc.
> >
> >--
> >Tibor Karaszi, SQL Server MVP
> >Archive at:
> >http://groups.google.com/groups?
> oi=djq&as_ugroup=microsoft.public.sqlserver
> >
> >
> >"SQL'''?"
> <anonymous@.discussions.microsoft.com> wrote in
> >message news:368b01c3aa26$9a6deb80$a601280a@.phx.gbl...
> >> When trying to do a backup, I come up with this message.
> >>
> >> Backup, CHECKALLOC, bulk copy, SELECT INTO, and file
> >> manipulation (such as CREATE FILE) operations on a
> >> database must be serialized. Reissue the statement after
> >> the current backup, CHECKALLOC, or file manipulation
> >> operation is completed. [SQLSTATE 42000] (Error 3023)
> >> Backup or restore operation terminating abnormally.
> >> [SQLSTATE 42000] (Error 3013). The step failed.
> >>
> >> What could be causing this failed job?
> >
> >
> >.
> >