Click here to Skip to main content
15,887,945 members
Home / Discussions / Database
   

Database

 
Questionemeregency Pin
yousefshokati1-Nov-11 18:43
yousefshokati1-Nov-11 18:43 
AnswerRe: emeregency Pin
Mycroft Holmes1-Nov-11 19:15
professionalMycroft Holmes1-Nov-11 19:15 
GeneralRe: emeregency Pin
yousefshokati1-Nov-11 20:23
yousefshokati1-Nov-11 20:23 
GeneralRe: emeregency Pin
Mycroft Holmes1-Nov-11 20:58
professionalMycroft Holmes1-Nov-11 20:58 
GeneralRe: emeregency Pin
yousefshokati1-Nov-11 21:17
yousefshokati1-Nov-11 21:17 
QuestionStored Procedure to Handle CSV Pin
Franklin Smith31-Oct-11 0:32
Franklin Smith31-Oct-11 0:32 
AnswerRe: Stored Procedure to Handle CSV Pin
Mycroft Holmes31-Oct-11 1:11
professionalMycroft Holmes31-Oct-11 1:11 
GeneralRe: Stored Procedure to Handle CSV Pin
Franklin Smith31-Oct-11 2:07
Franklin Smith31-Oct-11 2:07 
I know the bulk insert won't work on the CSV itself. I know that the below code is incorrect to do much of what I want. Problem is I don't know how make it all work together.

The first part should work as is, I just need to pass the file name to the procedure.

Edited the first part here to actually work. Plus adding in all the normalization updates I know need to be there.
SQL
CREATE TABLE [dbo].[TempCsvImport](
    [RealEstateNumber] [nvarchar](11) NOT NULL,
    [Name] [nvarchar](100) NOT NULL,
    [StreetNumber] [nvarchar](10) NOT NULL,
    [StreetName] [nvarchar](50) NOT NULL,
    [Type] [nvarchar](10) NOT NULL,
    [Direction] [nvarchar](10) NULL,
    [Unit] [nvarchar](20) NULL,
    [City] [nvarchar](20) NOT NULL,
    [ZipCode] [nvarchar](10) NOT NULL)
GO 

SET QUOTED_IDENTIFIER OFF

DECLARE @fn varchar(8000)
SELECT @fn = 'C:\Users\owner\Documents\Visual Studio 2010\Projects\UPA Data Miner\UPA Data Miner\bin\Debug\634556001928942124.csv' 
DECLARE @cmd varchar(8000)
SET @cmd = "BULK INSERT dbo.[TempCsvImport] FROM '" + @fn + "' WITH(CODEPAGE='RAW',FIELDTERMINATOR = ',', ROWTERMINATOR='\n')"

EXEC(@cmd)

GO

UPDATE [dbo].[TempCsvImport]
   SET [ZipCode] = SUBSTRING(ZipCode,1,5)
 WHERE LEN([ZipCode]) != 5
 
GO

UPDATE [dbo].TempCsvImport
SET RealEstateNumber = REPLACE(RealEstateNumber,"-","")
WHERE CHARINDEX("-",RealEstateNumber) != 0


The next part is where I have no clue how to make it all work. The '@' parameters all have information from a record in the TempCsvImport



SQL
/*For each record in TempCsvImport*/

SELECT * FROM [dbo].[PropertyLookupInformation]
WHERE [dbo].[PropertyLookupInformation].[RealEstateNumber] = @RealEstateNumber
/*If a record is found*/
UPDATE [dbo].[PropertyLookupInformation]
   SET [Name] = @Name
      ,[StreetNumber] = @StreetNumber
      ,[StreetName] = @StreetName
      ,[Type] = @Type
      ,[Direction] = @Direction
      ,[Unit] = @Unit
      ,[City] = @City
      ,[ZipCode] = @ZipCode
      ,[LastReviewed] = CURRENT_TIMESTAMP
 WHERE RealEstateNumber = @RealEstateNumber

/*Otherwise insert*/ 

 INSERT INTO [dbo].[PropertyLookupInformation]
           ([PropertyID]
           ,[RealEstateNumber]
           ,[Name]
           ,[StreetNumber]
           ,[StreetName]
           ,[Type]
           ,[Direction]
           ,[Unit]
           ,[City]
           ,[ZipCode]
           ,[LastReviewed])
     VALUES
           ( NEWID()
           ,@RealEstateNumber
           ,@Name
           ,@StreetNumber
           ,@StreetName
           ,@Type
           ,@Direction
           ,@Unit
           ,@City
           ,@ZipCode
           ,CURRENT_TIMESTAMP)


Finally

SQL
/*Drop the temporary table.*/

DROP TABLE [dbo].[TempCsvImport]


If I have to do this all from the program, that is fine. I'd just rather use the database cause it is a lot faster internally than I can do the processing externally.

modified 31-Oct-11 9:15am.

GeneralRe: Stored Procedure to Handle CSV Pin
Mycroft Holmes31-Oct-11 11:58
professionalMycroft Holmes31-Oct-11 11:58 
GeneralRe: Stored Procedure to Handle CSV Pin
Franklin Smith31-Oct-11 22:02
Franklin Smith31-Oct-11 22:02 
GeneralRe: Stored Procedure to Handle CSV Pin
Mycroft Holmes31-Oct-11 22:52
professionalMycroft Holmes31-Oct-11 22:52 
AnswerRe: Stored Procedure to Handle CSV Pin
PIEBALDconsult1-Nov-11 3:00
mvePIEBALDconsult1-Nov-11 3:00 
Questionmysql client mobile app Pin
Jassim Rahma30-Oct-11 4:58
Jassim Rahma30-Oct-11 4:58 
QuestionFill gaps in rates data with previous working day rates. Pin
Dev S30-Oct-11 3:00
Dev S30-Oct-11 3:00 
AnswerRe: Fill gaps in rates data with previous working day rates. Pin
Mycroft Holmes30-Oct-11 12:52
professionalMycroft Holmes30-Oct-11 12:52 
QuestionGrant a role to another role Pin
Danzy8329-Oct-11 12:31
Danzy8329-Oct-11 12:31 
QuestionJust installed SQL Server 2008 R2 - Where is the app to create/open/add/update/delete DB file? Pin
swampwiz28-Oct-11 0:40
swampwiz28-Oct-11 0:40 
AnswerRe: Just installed SQL Server 2008 R2 - Where is the app to create/open/add/update/delete DB file? Pin
Geoff Williams28-Oct-11 1:29
Geoff Williams28-Oct-11 1:29 
AnswerRe: Just installed SQL Server 2008 R2 - Where is the app to create/open/add/update/delete DB file? Pin
phil.o28-Oct-11 2:25
professionalphil.o28-Oct-11 2:25 
QuestionNeed help with update SQL with subquery Pin
Hypermommy26-Oct-11 3:14
Hypermommy26-Oct-11 3:14 
AnswerRe: Need help with update SQL with subquery Pin
Corporal Agarn26-Oct-11 3:46
professionalCorporal Agarn26-Oct-11 3:46 
AnswerRe: Need help with update SQL with subquery Pin
Hypermommy26-Oct-11 6:03
Hypermommy26-Oct-11 6:03 
QuestionSELECT ... GROUP BY Error Pin
MikeDhaan26-Oct-11 1:07
MikeDhaan26-Oct-11 1:07 
AnswerRe: SELECT ... GROUP BY Error Pin
Simon_Whale26-Oct-11 1:13
Simon_Whale26-Oct-11 1:13 
GeneralRe: SELECT ... GROUP BY Error Pin
MikeDhaan26-Oct-11 1:59
MikeDhaan26-Oct-11 1:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.