SQL Server 2008 - RS -Bulk Insert

I'am trying to import some flat files to SQL using the following bulk insert:
cREATE TABLE #temp1
    [field1] [varchar](20) NOT NULL,
    [field2] [datetime] NOT NULL,
    [fields3] [varchar](100) not null
select * from #temp1
BULK
INSERT #temp1
FROM 'c:\testestes.txt' 
WITH
FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n',
FIRSTROW = 1
GO
INSERT INTO dbo.teste1 ( M_nAME, [Date], Notes)
Select RTRIM(LTRIM([field1])), RTRIM(LTRIM([field2])), RTRIM(LTRIM([fields3])) From #temp1
IF EXISTS(SELECT * FROM #temp1) drop table #temp1
And here is an example of my flat file:
TESTES11;19-03-2015 16:03:07
However, some rows have a third column with this aspect:
TESTES12;27-03-2015 18:03:32;Request timed out.
And I'm having some issues to import the second and third column to the table that I created (#temp1) because it don't allows me to import a datetime data.

One solution: import the line as whole into a staging table column. Process it further from the staging table.
Example of importing an entire line:
http://www.sqlusa.com/bestpractices2005/notepad/
Kalman Toth Database & OLAP Architect
SQL Server 2014 Database Design
New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014

Similar Messages

  • SQL Server 2012 Express bulk Insert flat file 1million rows with "" as delimeter

    Hi,
    I wanted to see if anyone can help me out. I am on SQL server 2012 express. I cannot use OPENROWSET because my system is x64 and my Microsoft office suit is x32 (Microsoft.Jet.OLEDB.4.0).
    So I used Import wizard and is not working either. 
    The only thing that let me import this large file, is:
    CREATE TABLE #LOADLARGEFLATFILE
    Column1
    varchar(100), Column2 varchar(100), Column3 varchar(100),
    Column4 nvarchar(max)
    BULK INSERT
    #LOADLARGEFLATFILE
    FROM 'C:\FolderBigFile\LARGEFLATFILE.txt'
    WITH 
    FIRSTROW = 2,
    FIELDTERMINATOR ='\t',
    ROWTERMINATOR ='\n'
    The problem with CREATE TABLE and BULK INSERT is that my flat file comes with text qualifiers - "". Is there a way to prevent the quotes "" from loading in the bulk insert? Below is the data. 
    Column1
    Column2
    Column3
    Column4
    "Socket Adapter"
    8456AB
    $4.25
    "Item - Square Drive Socket Adapter | For "
    "Butt Splice" 
    9586CB
    $14.51
    "Item - Butt Splice"
    "Bleach"
    6589TE
    $27.30
    "Item - Bleach | Size - 96 oz. | Container Type"
    Ed,
    Edwin Lopera

    Hi lgnusLumen,
    According to your description, you use BULK INSERT to import data from a data file to the SQL table. However, to be usable as a data file for bulk import, a CSV file must comply with the following restrictions:
    1. Data fields never contain the field terminator.
    2. Either none or all of the values in a data field are enclosed in quotation marks ("").
    In your data file, the quotes aren't consistent, if you want to prevent the quotes "" from loading in the bulk insert, I recommend you use SQL Server Import and Export Wizard tools in SQL Server Express version. area, it will allow to strip the
    double quote from columns, you can review the following screenshot.
    In other SQL Server version, we can use SQL Server Integration Services (SSIS) to import data from a flat file (.csv) with removing the double quotes. For more information, you can review the following article.
    http://www.mssqltips.com/sqlservertip/1316/strip-double-quotes-from-an-import-file-in-integration-services-ssis/
    In addition, you can create a function to convert a CSV to a usable format for Bulk Insert. It will replace all field-delimiting commas with a new delimiter. You can then use the new field delimiter instead of a comma. For more information, see:
    http://stackoverflow.com/questions/782353/sql-server-bulk-insert-of-csv-file-with-inconsistent-quotes
    Regards,
    Sofiya Li
    Sofiya Li
    TechNet Community Support

  • SQL Server 2008 - RS - Loop of multiple Bulk Inserts

    Hi,
    I want to import multiple flat files to a table on SQL Server 2008 R2. However, I don't have access to Integration Services to use a foreach loop, so I'm doing the process using T-SQL. Actually, I'm using manually code to which file to introduce the data on
    tables. My code are like this:
    cREATE TABLE #temporaryTable
        [column1] [varchar](100) NOT NULL,
        [column2 [varchar](100) NOT NULL
    BULK
    INSERT #temp
    FROM 'C:\Teste\testeFile01.txt' 
    WITH
    FIELDTERMINATOR = ';',
    ROWTERMINATOR = '\n',
    FIRSTROW = 1
    GO
    BULK
    INSERT #temp
    FROM 'C:\Teste\testeFile02.txt' 
    WITH
    FIELDTERMINATOR = ';',
    ROWTERMINATOR = '\n',
    FIRSTROW = 1
    GO
    -------------------------------------------------INSERT INTO dbo.TESTE ( Col_1, Col_2)
    Select RTRIM(LTRIM([column1])), RTRIM(LTRIM([column2])) From #temporaryTable
    IF EXISTS(SELECT * FROM #temporaryTable) drop table #temporaryTable
    The problem is that I have 20 flat files to Insert... Do I have any loop solution in T-SQL to insert all the flat files on same table?
    Thanks!

    Here is a working sample of powershell script I adopted from internet( I don't have the source handy now).
    Import-Module -Name 'SQLPS' -DisableNameChecking
    $workdir="C:\temp\test\"
    $svrname = "MC\MySQL2014"
    Try
    #Change default timeout time from 600 to unlimited
    $svr = new-object ('Microsoft.SqlServer.Management.Smo.Server') $svrname
    $svr.ConnectionContext.StatementTimeout = 0
    $table="test1.dbo.myRegions"
    #remove the filename column in the target table
    $q1 = @"
    Use test1;
    IF COL_LENGTH('dbo.myRegions','filename') IS NOT NULL
    BEGIN
    ALTER TABLE test1.dbo.myRegions DROP COLUMN filename;
    END
    Invoke-Sqlcmd -ServerInstance $svr.Name -Database master -Query $q1
    $dt = (get-date).ToString("yyyMMdd")
    $formatfilename="$($table)_$($dt).xml"
    $destination_formatfilename ="$($workdir)$($formatfilename)"
    $cmdformatfile="bcp $table format nul -c -x -f $($destination_formatfilename) -T -t\t -S $($svrname) "
    Invoke-Expression $cmdformatfile
    #Delay 1 second
    Start-Sleep -s 1
    $q2 = @"
    Alter table test1.dbo.myRegions Add filename varchar(500) Null;
    #add the filename column to the target table
    Invoke-Sqlcmd -ServerInstance $svr.Name -Database master -Query $q2
    $files = Get-ChildItem $workdir
    $items = $files | Where-Object {$_.Extension -eq ".txt"}
    for ($i=0; $i -lt $items.Count; $i++) {
    $strFileName = $items[$i].Name
    $strFileNameNoExtension= $items[$i].BaseName
    $query = @"
    BULK INSERT test1.dbo.myRegions from '$($workdir)$($strFileName)' WITH (FIELDTERMINATOR = '\t', FIRSTROW = 2, FORMATFILE = '$($destination_formatfilename)');
    Invoke-Sqlcmd -ServerInstance $svr.Name -Database master -Query $query -querytimeout 65534
    #Delay 10 second
    Start-Sleep -s 10
    # Update the filename column
    Invoke-Sqlcmd -ServerInstance $svr.Name -Database master -querytimeout 65534 -Query "Update test1.dbo.myRegions SET filename= '$($strFileName)' WHERE filename is null; "
    # Move uploaded file to archive
    If ((Test-Path "$($workdir)$($strFileName)") -eq $True) { Move-Item -Path "$($workdir)$($strFileName)" -Destination "$($workdir)Processed\$($strFileNameNoExtension)_$($dt).txt"}
    Catch [Exception]
    write-host "--$strFileName "$_.Exception.Message

  • MS SQL server 2008 - Bulk copy from XML to DB table throws bcp_init error

    I have MS SQL server 2008 installed ,
    Windows version - Windows 7 Professional with SP1
    Doing a bulk copy process using the SQL library function bcp_init function in c++ throws xml error and its not inserting the data into the tables.
    Error received ,
    XML Datatransfer error: XML data or another error occurred while reading file 'd:\temp\scripts\dbtoolscripts\table_data.xml':
    But this works in others machines with the same windows version and SQL version.

    We are using the same SQL lib function bcp_init , we have written a separate class to load the ODBC32.dll , sqlncli.dll  and use the  bcp_init and other SQL functions from that. As i mentioned earlier we all use the same application build , but
    its not working in my machine only other machines its working.
    There is no provision to load XML files when you use the BCP interface in sqlncli.dll. You can of course load XML files, but the BCP API does not know that it is XML, it only sees a number of bytes.
    So that error message is not coming from the BCP API, but somewhere else. Maybe your own code in reaction to some error from the BCP API. But without any clue of that error message, we can't help you.
    I think you will need to do some debugging or by some other means improve your diagnostics.
    Erland Sommarskog, SQL Server MVP, [email protected]

  • Insert missing records dynamically in SQL Server 2008 R2

    Hi, I am working on a requirement where I will have to work on some % calculations for 2 different states where we do business. The task that I am trying to accomplish is technically we need to have any Product sales in both the states if not for instance
    in the following example ProductC does not have sales in OR so I need to insert a record for ProductC - OR with 0. This is an intermediary resultset of all the calculations that I am working with.
    Example Dataset:
    CREATE TABLE [dbo].[Product](
    [Product] [varchar](18) NOT NULL,
    [State] [varchar](5) NOT NULL,
    [Area1] [int] NOT NULL,
    [Area2] [int] NOT NULL,
    [Area3] [int] NOT NULL,
    [Area4] [int] NOT NULL,
    [Area5] [int] NOT NULL,
    [Area6] [int] NOT NULL,
    [Area7] [int] NOT NULL,
    [Area8] [int] NOT NULL,
    [Area9] [int] NOT NULL,
    [Area10] [int] NOT NULL,
    [Total] [int] NULL
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductA','OR',0,0,2,0,2,0,0,0,0,0,4)
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductA','WA',0,0,1,0,0,0,0,0,0,0,1)
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductA','Total',0,0,3,0,2,0,0,0,0,0,5)
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductB','OR',0,0,5,0,0,0,0,0,0,0,5)
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductB','WA',0,0,2,0,0,1,0,0,0,0,3)
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductB','Total',0,0,7,0,0,1,0,0,0,0,8)
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductC','WA',0,0,0,0,0,0,0,0,0,1,1)
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductC','Total',0,0,0,0,0,0,0,0,0,1,1)
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductD','OR',5,2,451,154,43,1,0,0,0,0,656)
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductD','WA',0,20,102,182,58,36,0,1,0,0,399)
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductD','Total',5,22,553,336,101,37,0,1,0,0,1055)
    How to accomplish this in SQL Server 2008 R2.
    Insert missing record:
    INSERT INTO [Product] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductC','OR',0,0,0,0,0,0,0,0,0,0,0)
    Thanks in advance......
    Ione

    CREATE TABLE [dbo].[Products](
    [Product] [varchar](18) NOT NULL,
    [State] [varchar](5) NOT NULL,
    [Area1] [int] NOT NULL,
    [Area2] [int] NOT NULL,
    [Area3] [int] NOT NULL,
    [Area4] [int] NOT NULL,
    [Area5] [int] NOT NULL,
    [Area6] [int] NOT NULL,
    [Area7] [int] NOT NULL,
    [Area8] [int] NOT NULL,
    [Area9] [int] NOT NULL,
    [Area10] [int] NOT NULL,
    [Total] [int] NULL
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductA','OR',0,0,2,0,2,0,0,0,0,0,4)
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductA','WA',0,0,1,0,0,0,0,0,0,0,1)
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductA','Total',0,0,3,0,2,0,0,0,0,0,5)
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductB','OR',0,0,5,0,0,0,0,0,0,0,5)
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductB','WA',0,0,2,0,0,1,0,0,0,0,3)
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductB','Total',0,0,7,0,0,1,0,0,0,0,8)
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductC','WA',0,0,0,0,0,0,0,0,0,1,1)
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductC','Total',0,0,0,0,0,0,0,0,0,1,1)
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductD','OR',5,2,451,154,43,1,0,0,0,0,656)
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductD','WA',0,20,102,182,58,36,0,1,0,0,399)
    INSERT INTO [Products] ([Product],[State],[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])VALUES('ProductD','Total',5,22,553,336,101,37,0,1,0,0,1055)
    ;WIth mycte as (
    Select Product,State From (Select distinct Product from Products) p, (
    Select distinct State from Products) s
    Insert into [Products](Product,State,[Area1],[Area2],[Area3],[Area4],[Area5],[Area6],[Area7],[Area8],[Area9],[Area10],[Total])
    Select m.Product,m.State
    ,0,0,0,0,0,0,0,0,0,0,0
    from mycte m Left Join Products p on m.[Product]=p.Product and m.State=p.State
    WHERE p.Product is null and p.State is null
    select * from Products
    Order by Product
    ,Case when State='Total' Then 2 else 1 End
    Drop table Products

  • Insert data from SQL Server 2008 St.Ed to ORACLE 11gr2 St. Edition

    Dear all,
    I'm searching a solution for faster transmission of data of a db SQL Server (2008 St. edition) vs PROD db that is an ORACLE 11gr2 St. edition too.
    Actually this operation spend 1,5 hours but I'd like cut this time because size of the data grow up in future and this time could be a serious problem ....
    Looking on Oracle Enterprise Manager I've seen that main problem is about commit .. SQL Server every record insert into Oracle TBL generate a commit  ... unfortunately I cannot insert when commit must be done ... If will be solved it probably may be solved situation ...
    Thanks in advance for every idea.
    Andrea

    Nardo wrote:
    SQL server is connect with Oracle DB by OLEDB, commit every record is why SQL haven't possibility of manage it ... anyway I'm sorry for delay of the answer, I've solved using sqlloader ... faster and without impact ... must be have hight attention if there will be change any field ... that's all.
    REDO upgrade was done like first step because anyway it was too small.
    Thanks for you answer and sorry again for delay !!!!
    Thanks.
    If you are depending on MS SQL server technology to do this, you are going to be stuck with the performance of that technology.
    A few weeks ago we had a need to load institute a daily load of or DW from a MSSQL database.  The MS-centric person responsible developed a procedure (using MS tools) that took 4.5 hours to load 1.2 million rows.  I ran a session trace and the tkprof report revealed exactly what has already been described.  Single row insert and commit.  I knocked together a PL/SQL procedure that did a single INSERT .. (SELECT .. FROM TABLE@MSSQL). and it ran in 4 minutes.  Using the Oracle transparent gateway for ODBC.

  • Inserting data from Oracle to SQL Server 2008

    Hi All,
    I am using oracle 9.2.0.7 and SQL server 2008 in windows server 2003 operating system.
    My requirment is like below.
    1) I would liek to insert data from the Oracle database to SQL Server.
    For the above requirement we have created the ODBC, then configure the listener file, tns file, and created the db link. This part is working fine as I am able to select the data from the SQL Server.
    But when I am inserting the record from the oracle database to the SQL Server I am getting the error, (ORA-00904) Here oracle can not identified the column name.
    My insert statement is like below.
    insert into test@SQLTEST (name, fullname)
    values ('Oracle', 'Oracle server data');
    Test is the SQL server table name and sqltest is the db link name.
    If I am select the data
    select * from test@SQLTEST from Oralce I am getting the data properly.
    Thanks,
    SUN
    Edited by: User SUN@ on Jun 9, 2010 5:49 PM

    Hi,
    I modify the column name as "f_name" and "l_name";
    Still I am getting the same error;
    Thanks,
    SUN

  • How can this encoding be saved in SQL Server 2008

    hi all,
    We are using Sql Server 2008, it has this collation set "SQL_Latin1_General_CP1_CI_AS". Please have a look at this file:
    https://drive.google.com/file/d/0BxWAyvJA9ZjCdkFNN3BXZUw3dEE/view?pli=1
    its a text file in text format (ascii test data format aka ATDF). Its coming from French vendor and it contains some French words. We have to parse this and upload data to sql server. We do that all the time except this file is not working. It just disturbs
    the way sql server is saving data for us, and upon showing this saved data on application UI, the correct letters don't display. The upload happens via the Bulk Insert command. Please guide as to where to start. Also please let me know exactly what this format
    is called, do we call it just unicodes, or some utf formats. I'm really ignorant when it comes to unicode. Thanks a lot!
    ..ab
    Ab

    Hi
    Please use Unicode data types on the SQL Server side. Please see the following document:
    http://msdn.microsoft.com/en-us/library/ms187828(v=SQL.105).aspx 
    Thanks
    Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers

  • Msg 8631 Internal error: Server stack limit has been reached on SQL Server 2012 from T-SQL script that runs on SQL Server 2008 R2

    I have an Script mostly that is generated by SSMS which works with-out issue on SQL Server 2008, but when I attempt to run it on a new fresh install of SQL Server 2012 I get an Msg 8631. Internal error: Server stack limit has been reached. Please look for
    potentially deep nesting in your query, and try to simplify it.
    The script itself doesn't seem to be all that deep or nested.  The script is large 2600 lines and when I remove the bulk of the 2600 lines, it does run on SQL Server 2012.  I'm just really baffled why something that SQL Server generated with very
    few additions/changes AND that WORKS without issue in SQL Server 2008 R2 would suddenly be invalid in SQL Server 2012
    I need to know why my script which is working great on our current SQL Server 2008 R2 servers suddenly fails and won't run on an new SQL Server 2012 server.  This script is used to create 'bulk' Replications on a large number of DBs saving a tremendous
    amount of our time doing it the manual way.
    Below is an 'condensed' version of the script which fails.  I have removed around 2550 lines of specific sp_addarticle statements which are mostly just copy and pasted from what SQL Management Studio 'scripted' for me went I when through the Replication
    Wizard and told it to save to script.
    declare @dbname varchar(MAX), @SQL nvarchar(MAX)
    declare c_dblist cursor for
    select name from sys.databases WHERE name like 'dbone[_]%' order by name;
    open c_dblist
    fetch next from c_dblist into @dbname
    while @@fetch_status = 0
    begin
    print @dbname
    SET @SQL = 'DECLARE @dbname NVARCHAR(MAX); SET @dbname = ''' + @dbname + ''';
    use ['+@dbname+']
    exec sp_replicationdboption @dbname = N'''+@dbname+''', @optname = N''publish'', @value = N''true''
    use ['+@dbname+']
    exec ['+@dbname+'].sys.sp_addlogreader_agent @job_login = N''DOMAIN\DBServiceAccount'', @job_password = N''secret'', @publisher_security_mode = 1, @job_name = null
    -- Adding the transactional publication
    use ['+@dbname+']
    exec sp_addpublication @publication = N'''+@dbname+' Replication'', @description = N''Transactional publication of database
    '''''+@dbname+''''' from Publisher ''''MSSQLSRV\INSTANCE''''.'', @sync_method = N''concurrent'', @retention = 0, @allow_push = N''true'', @allow_pull = N''true'', @allow_anonymous = N''false'', @enabled_for_internet
    = N''false'', @snapshot_in_defaultfolder = N''true'', @compress_snapshot = N''false'', @ftp_port = 21, @allow_subscription_copy = N''false'', @add_to_active_directory = N''false'', @repl_freq = N''continuous'', @status = N''active'', @independent_agent = N''true'',
    @immediate_sync = N''true'', @allow_sync_tran = N''false'', @allow_queued_tran = N''false'', @allow_dts = N''false'', @replicate_ddl = 1, @allow_initialize_from_backup = N''true'', @enabled_for_p2p = N''false'', @enabled_for_het_sub = N''false''
    exec sp_addpublication_snapshot @publication = N'''+@dbname+' Replication'', @frequency_type = 1, @frequency_interval = 1, @frequency_relative_interval = 1, @frequency_recurrence_factor = 0, @frequency_subday = 8,
    @frequency_subday_interval = 1, @active_start_time_of_day = 0, @active_end_time_of_day = 235959, @active_start_date = 0, @active_end_date = 0, @job_login = N''DOMAIN\DBServiceAccount'', @job_password = N''secret'', @publisher_security_mode = 1
    -- There are around 2400 lines roughly the same as this only difference is the tablename repeated below this one
    use ['+@dbname+']
    exec sp_addarticle @publication = N'''+@dbname+' Replication'', @article = N''TABLE_ONE'', @source_owner = N''dbo'', @source_object = N''TABLE_ONE'', @type = N''logbased'', @description = null, @creation_script =
    null, @pre_creation_cmd = N''drop'', @schema_option = 0x000000000803509F, @identityrangemanagementoption = N''manual'', @destination_table = N''TABLE_ONE'', @destination_owner = N''dbo'', @vertical_partition = N''false'', @ins_cmd = N''CALL sp_MSins_dboTABLE_ONE'',
    @del_cmd = N''CALL sp_MSdel_dboTABLE_ONE'', @upd_cmd = N''SCALL sp_MSupd_dboTABLE_ONE''
    EXEC sp_executesql @SQL
    SET @dbname = REPLACE(@dbname, 'dbone_', 'dbtwo_');
    print @dbname
    SET @SQL = 'DECLARE @dbname NVARCHAR(MAX); SET @dbname = ''' + @dbname + ''';
    use ['+@dbname+']
    exec sp_replicationdboption @dbname = N'''+@dbname+''', @optname = N''publish'', @value = N''true''
    use ['+@dbname+']
    exec ['+@dbname+'].sys.sp_addlogreader_agent @job_login = N''DOMAIN\DBServiceAccount'', @job_password = N''secret'', @publisher_security_mode = 1, @job_name = null
    -- Adding the transactional publication
    use ['+@dbname+']
    exec sp_addpublication @publication = N'''+@dbname+' Replication'', @description = N''Transactional publication of database
    '''''+@dbname+''''' from Publisher ''''MSSQLSRV\INSTANCE''''.'', @sync_method = N''concurrent'', @retention = 0, @allow_push = N''true'', @allow_pull = N''true'', @allow_anonymous = N''false'', @enabled_for_internet
    = N''false'', @snapshot_in_defaultfolder = N''true'', @compress_snapshot = N''false'', @ftp_port = 21, @allow_subscription_copy = N''false'', @add_to_active_directory = N''false'', @repl_freq = N''continuous'', @status = N''active'', @independent_agent = N''true'',
    @immediate_sync = N''true'', @allow_sync_tran = N''false'', @allow_queued_tran = N''false'', @allow_dts = N''false'', @replicate_ddl = 1, @allow_initialize_from_backup = N''true'', @enabled_for_p2p = N''false'', @enabled_for_het_sub = N''false''
    exec sp_addpublication_snapshot @publication = N'''+@dbname+' Replication'', @frequency_type = 1, @frequency_interval = 1, @frequency_relative_interval = 1, @frequency_recurrence_factor = 0, @frequency_subday = 8,
    @frequency_subday_interval = 1, @active_start_time_of_day = 0, @active_end_time_of_day = 235959, @active_start_date = 0, @active_end_date = 0, @job_login = N''DOMAIN\DBServiceAccount'', @job_password = N''secret'', @publisher_security_mode = 1
    -- There are around 140 lines roughly the same as this only difference is the tablename repeated below this one
    use ['+@dbname+']
    exec sp_addarticle @publication = N'''+@dbname+' Replication'', @article = N''DB_TWO_TABLE_ONE'', @source_owner = N''dbo'', @source_object = N''DB_TWO_TABLE_ONE'', @type = N''logbased'', @description = null, @creation_script
    = null, @pre_creation_cmd = N''drop'', @schema_option = 0x000000000803509D, @identityrangemanagementoption = N''manual'', @destination_table = N''DB_TWO_TABLE_ONE'', @destination_owner = N''dbo'', @vertical_partition = N''false''
    EXEC sp_executesql @SQL
    fetch next from c_dblist into @dbname
    end
    close c_dblist
    deallocate c_dblist
    George P Botuwell, Programmer

    Hi George,
    Thank you for your question. 
    I am trying to involve someone more familiar with this topic for a further look at this issue. Sometime delay might be expected from the job transferring. Your patience is greatly appreciated. 
    Thank you for your understanding and support.
    If you have any feedback on our support, please click
    here.
    Allen Li
    TechNet Community Support

  • Data Committed instead of Rollback after Deadlock Error in SQL Server 2008 R2 (SP2)

    We're having a strange issue which is occurring only with one Customer having SQL Server 2008 R2 (SP2).
    Basically we have multiple threads uploading data and when an error occurs (like deadlock or any other error). The deadlock victim (process/transaction) is rolledback (from .NET). However the rollback command is not reaching SQL Server as it doesn't show
    in the trace (through SQL Profiler).
    To make things worse, not only the transaction is not being rolled back but the INSERTs executed before the error are being somehow committed, leaving the database in an inconsistent state.
    This is only produced in one environment.
    Any idea what the issue could be?

    All statements are executed with in a Transaction. Under the same scenario this code works perfectly fine for 1000s of customers. Only one customer has this issue.
    You need to capture profiler to check transaction scope.
    Balmukund Lakhani
    Please mark solved if I've answered your question, vote for it as helpful to help other users find a solution quicker
    This posting is provided "AS IS" with no warranties, and confers no rights.
    My Blog |
    Team Blog | @Twitter
    | Facebook
    Author: SQL Server 2012 AlwaysOn -
    Paperback, Kindle

  • How to configure Sql Server 2008 R2 for debugging without disabling Windows Firewall?

    When I try to debug a procedure in Sql Server 2008 R2 I get a dialog box telling me to unblock some ports.
    >>
    The Windows Firewall on this machine is currently blocking remote debugging.  Remote debugging requiers that the debugger be allowed to receive informatino from the network.  Remote debugging also requires DCOM (TCP Port 135) and IPSEC (UDP 4500
    / UDP 500) be unblocked.
    <<
    I went to the Windows Firewall and added some new Inbound rules for each of the specified ports (in the images below.  But after this and rebooting entire computer -- I still get this dialog box when I want to go into debug mode from SSMS.  I can
    actually kind of debug standalone code in the SSMS window if I cancel the dialog box.  But I can't debug an actual stored procedure -- which is what I need to do.  I also noticed that my server has an IP address instead of LocalHost.  This is
    not a public Server -- it's my private development machine which is on a peer to peer network (with one other workstation), but I don't need that workstation to be able to access this server..  Would localholst make a difference for debugging?  How
    should I configure the sql server?  The firewall?
    Rich P

    Use PRINT and SELECT for debugging. That's what I do. I used to play with the debugger in SQL 2000 and older days, but I realised that I spent more time on getting it to work that I gained from using it.
    I thought about that.  I guess that IS the way to go.  Very well.  Thank you for the suggestion.   Although, when the debugger does work in the regular window (not a stored proc because it doesn't work there) the debugger IS nice. 
    For debugging my proc I am thinking I will insert param vals into a test collection table for now.  One obscure error I am getting with a particular proc -- in a When loop on one parameter I get the "value will be truncated" error -- but everything
    is set to accept 1000 characters and none of the source data even comes close to 1000 characters.  I have isolated at what point the error occurs and which section of data where the error is occurring but I have not isolated the cause of the error. 
    I cannot reproduce the error when I run the procedure by hand in a regular query window with the exact same code.  The error only occurs when the code is run in the stored proc.
    Rich P

  • Adobe PDF iFilter SQL Server 2008 R2 Full Text not working

    Unable to get the SQL Server 2008 R2 to index PDF files for full-text searching.
    Environment: Windows 7 SP1 Enterprise 64-bit, SQL Server 2008 R2 Express SP3 64-bit, Adobe Reader 11.x
    Installed PDF iFilter 64 (11.0.01) from
    http://www.adobe.com/support/downloads/detail.jsp?ftpID=5542
    Added its folder (C:\Program Files\Adobe\Adobe PDF iFilter 11 for 64-bit platforms\bin\;) to the PC system Path variable. Rebooted PC.
    Confirmed SQL Server sees the Adobe PDF iFilter ..... SELECT * from sys.fulltext_document_types where document_type = '.pdf' 
    Inserted PDF files to my table (see below for CREATE statement).
    The SQLFT log says  ... Warning: No appropriate filter was found during full-text index population for table or indexed view '[TestDB].[dbo].[pdfifiltertable]' (table or indexed view ID '2105058535', database ID '7'), full-text key value '1'.
    Some columns of the row were not indexed.
    Installed other filters for Office. Added files of type DOC, XPS. Confirmed the search query works for those file types. So as such SQL Full-Text is enabled and working.
    Below my T-SQL commands:
    Exec sp_fulltext_service 'load_os_resources', 1
    Exec sp_fulltext_service 'verify_signature', 0
    Exec sp_fulltext_database 'enable'
    CREATE TABLE pdfifiltertable(
    PdfID INT IDENTITY NOT NULL,
    PdfFileName VARCHAR(MAX),
    Ext VARCHAR(10) ,
    PdfText VARBINARY(MAX),
    CONSTRAINT PK_PdfID PRIMARY KEY (PdfID)
    GO
    CREATE FULLTEXT CATALOG pdfCatalog AS DEFAULT
    GO
    CREATE FULLTEXT INDEX ON pdfifiltertable([PdfText] Type column [Ext]
    ) KEY INDEX PK_PdfID with change_tracking auto
    GO
    SELECT pdfFileName FROM pdfifiltertable WHERE CONTAINS(pdftext, 'payment')

    What edition are you using? Like is it SQL-Express or SQL-Standard etc.
    Adding files using C# desktop application.
    Tested in another PC SQL-Server Express 2008 with Windows 7 64-bit. Same issue.
    C# source code ... add a button called uploadPDF and a datagridview called datagridview to the form.
    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    namespace UploadPDF
    public partial class Form1 : Form
    #region Properties
    protected static string ConnectionString {
    get {return "Integrated Security=SSPI;database=Testdb;Data Source=localhost\\SQLEXPRESS;Workstation ID=localhost;";}
    protected static SqlConnection Connection {
    get {return new SqlConnection(ConnectionString);}
    protected DataTable pdfDataTable;
    #endregion
    public Form1()
    InitializeComponent();
    populateDataGrid();
    protected void populateDataGrid()
    SqlConnection con = Connection;
    con.Open();
    try
    pdfDataTable = new DataTable();
    SqlCommand cmd = new SqlCommand( "select * from pdfifiltertable", con);
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    adapter.Fill(pdfDataTable);
    dataGridView.DataSource = pdfDataTable;
    dataGridView.Columns[3].Visible = false;
    finally {
    con.Close();
    private void uploadPDF_Click(object sender, EventArgs e) {
    if (DialogResult.Cancel == fileOpenDialog.ShowDialog()) return;
    try {
    byte[] content = FileToByteArray(fileOpenDialog.FileName);
    uploadPDFBlob2DataBase(fileOpenDialog.FileName,content);
    populateDataGrid();
    } catch (Exception ex) {
    MessageBox.Show(ex.Message, "PDFiFilter11", MessageBoxButtons.OK, MessageBoxIcon.Error);
    public byte[] FileToByteArray(string _FileName) {
    byte[] pdfBuffer = null;
    try {
    System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
    long TotalNumberOfBytes = new System.IO.FileInfo(_FileName).Length;
    pdfBuffer = _BinaryReader.ReadBytes((Int32)TotalNumberOfBytes);
    _FileStream.Close();
    _FileStream.Dispose();
    _BinaryReader.Close();
    } catch (Exception _Exception) {
    Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
    return pdfBuffer;
    public void uploadPDFBlob2DataBase(String fName, byte[] content) {
    SqlConnection con = Connection;
    con.Open();
    String ext,filename;
    ext = Path.GetExtension(fName);
    filename = Path.GetFileName(fName);
    try {
    SqlCommand insert = new SqlCommand( "insert into pdfifiltertable ([PdfFileName],[Ext],[PdfText]) values ((@pdfFileName),(@extension),(@pdfcontent))", con);
    SqlParameter pdffilenameParameter = insert.Parameters.Add("@pdfFileName", SqlDbType.NText);
    pdffilenameParameter.Value = filename;
    pdffilenameParameter.Size = filename.Length;
    SqlParameter extParam = insert.Parameters.Add("@extension", SqlDbType.NVarChar);
    extParam.Value = ext;
    extParam.Size = ext.Length;
    SqlParameter pdfcontentParameter = insert.Parameters.Add("@pdfcontent", SqlDbType.Binary);
    pdfcontentParameter.Value = content;
    pdfcontentParameter.Size = content.Length;
    insert.ExecuteNonQuery();
    } finally {
    con.Close();
    } //class
    } //namespace

  • Execute a procedure with a list of array in sql server 2008

    HI all,
    I have a procedure which has a list of values passed as an array . How can i execute my procedure with array list? how to implement this?Please help me.
    Thanks in advance
    Deepa

    Hi Deepa,
    basically Microsoft SQL Server does not support arrays as data types for procedures. What you can do is creating a type which represents a table definition. This type can than be used in a procedure as the following demo will show:
    The first script creates the environment which will be used for the execution
    -- 1. create the table as a type in the database
    CREATE TYPE OrderItems AS TABLE
    ItemId int PRIMARY KEY CLUSTERED
    GO
    -- 2. demo table for demonstration of results
    CREATE TABLE dbo.CustomerOrders
    Id int NOT NULL IDENTITY (1, 1) PRIMARY KEY CLUSTERED,
    CustomerId int NOT NULL,
    ItemId int
    GO
    -- 3. Insert a few records in demo table
    INSERT INTO dbo.CustomerOrders
    (CustomerId, ItemId)
    VALUES
    (1, 1),
    (2, 1),
    (3, 3),
    (1, 3);
    GO
    -- 4. Create the procedure which accepts the table variable
    CREATE PROC dbo.OrderedItemsList
    @Orders AS OrderItems READONLY
    AS
    SET NOCOUNT ON;
    SELECT o.*
    FROM dbo.CustomerOrders AS O INNER JOIN @Orders AS T_O
    ON (o.ItemId = T_O.ItemId);
    SET NOCOUNT OFF;
    GO
    The above script creates the table data type and a demo table with a few demo data. The procedure will accept the table data type as parameter. Keep in mind that table variable parameters have to be READONLY as parameter for procedures!
    The second script demonstrates the usage of the above scenario
    When the environment has been created the usage is a very simple one as you can see from the next script...
    -- 1. Fill the variable table with item ids
    DECLARE @o AS OrderItems;
    INSERT INTO @o (ItemId)
    VALUES
    (1), (3);
    -- 2. Get the list of customers who bought these items
    EXEC dbo.OrderedItemsList @Orders = @o;
    MCM - SQL Server 2008
    MCSE - SQL Server 2012
    db Berater GmbH
    SQL Server Blog (german only)

  • User-Defined Table Types - Where is it in SQL Server 2008?

    I am having 2 issues:
    1.  I don't see a folder for 'User-Defined Table Types' under programmability-->Types
    2.  When I execute the following code to create a Type; I get an error 'Incorrect syntax near the keyword 'AS'.'
    USE Locations;
    GO
    /* Create a user-defined table type */
    CREATE TYPE LocationTableType AS TABLE
    ( LocationName VARCHAR(50)
    , CostRate INT );
    GO

    Hi Shayann,
    The  user-defined table type is a new
    feature from SQL Server 2008. If you want to use it you have to upgrade your database to SQL Server 2008 or above.
    In you scenario(SQL Server 2005),as a workaround, you can use the  local temporary table instead. 
    --in SQL Server 2005
    CREATE TABLE tblType(id INT,name VARCHAR(99))
    GO
    CREATE PROC proc1
    AS
    BEGIN
    SELECT * FROM #temp;
    END
    GO
    SELECT * INTO #temp FROM tblType; -- the #temp name is fixed
    INSERT INTO #temp VALUES(1,'Eric');
    EXEC proc1
    --In SQL Server 2008 or above
    CREATE TYPE LocationTableType AS TABLE
    ( id INT,
    name VARCHAR(99) );
    GO
    CREATE PROC proc1 @t LocationTableType READONLY
    AS
    BEGIN
    SELECT * FROM @t
    END
    GO
    DECLARE @t LocationTableType;
    INSERT INTO @t VALUES(1,'Eric');
    EXEC proc1 @t
    I still suggest you upgrade to higher SQL Server.
    If you have any question, feel free to let me know.
    Best regards,
    Eric

  • How to store Invalid Dates in SQL Server 2008

    Hi,
    I have SQL Server 2008 and My table has start_Date and end_date column which is DATETIME datatype.
    Somehow my table is having invalid dates which is lesser than 1753-01-01 00:00:00.0 range.
    I want to know how this date get inserted in the SQL Server. 
    Thanks,
    Muthuprabhu

    Can you post the DDL for that table together with a SELECT query and results demonstrating invalid data?
    DECLARE @dt DATETIME = '1753-01-01 00:00:00.0';
    -- Command(s) completed successfully.
    DECLARE @dt DATETIME = '1752-01-01 00:00:00.0';
    Msg 242, Level 16, State 3, Line 4
    The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
    Datetime conversion:
    http://www.sqlusa.com/bestpractices/datetimeconversion/
    Kalman Toth Database & OLAP Architect
    SQL Server 2014 Database Design
    New Book / Kindle: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2014

Maybe you are looking for

  • How do I create this color-picker-HSB gradient?

    What seemed trivial at first glance has turned out as quite a challange! I would like to create a special kind of black/white/alpha gradient to mimic the HSB color picker in Photoshop: Saturation 0 100 +--------------+ 100 |white.....pure hue| |.....

  • (CS4) (AS) Syntax for assigning existing Preflight Profile

    Building an AppleScript workflow to tie a FileMaker database to IDCS4, using Triple Triangle's Job Spec Cubed. Very fun. I'd like to add a tell statement to assign existing Preflight Profiles to existing documents. We have four standard preflight pro

  • What's the deal with FP update doesn't work!

    What's up with Update FP player?

  • How to know whether BPC addon ws with in that BI server or seperate

    Hello everyone! I am asking this Q from BASIS prespective I know that BPC can be done in seperate server but need to load data again to BPC server also Here my Q is how we can know that whether BPC is added on with in BI system because I am unable to

  • OSB Query

    Hi , OSB is acting different while i am testing my message flow in IE, FF,Chrome and SOAP-UI . i have simply assigned this expression using assign action fn:data($inbound/ctx:security/ctx:transportClient/ctx:username) to a dummy variable. while testi