Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Insert from tabbed file with a conditional and table lookup

$
0
0

I need help with a stored procedure.

I have two tablesCRIS_USER and CRIS_USER_ LOCATION.

CREATETABLE[dbo].[CRIS_USER] (

[id]numeric(10,0)NOTNULL,

[login_id]varchar(20)NOTNULL,

[active]numeric(1,0)NOTNULL,

[password]varchar(50)NULL,

[PW_DATE]datetimeNULL,

[DEFAULT_PASSWORD]varchar(50)NULL,

[FIRST_NAME]varchar(50)NULL,

[LAST_NAME]varchar(50)NULL,

[EMERGENCY_USER]numeric(10,0)NULL)

ON[PRIMARY]

CREATETABLE[dbo].[CRIS_USER_LOCATION] (

[id]numeric(10,0)NOTNULL,

[cris_user_id]numeric(10,0)NOTNULL,

[location_id]numeric(10,0)NOTNULL)

ON[PRIMARY]

My query will read a tabbed text file UserLocation.

Staff Name    login  121155 063071750

Obiwan Kenobi obikenX              

Luke Skywalker lsky  X              

Han Solo       hsolo    X           X

CRIS_USER.id is the foreign key  cris_user_id in CRIS_USER_LOCATION

The query reads UserLocation text file into #LocationTemp -  UserName, UserLogin, Loc_1, Loc_2, Loc_3,  Loc_4, Loc_5

CRIS_ USER_ LOCATION.location_id is a three digit number – 121, 155, 063, 071, or 750.

In the text file, Loc_1 thru Loc_5 will each have an “x” or a null value.

Loc_1 = 121

Loc_2 = 155

Loc_3 = 063

Loc_4 = 071

Loc_5 = 750

Locations with an “x” indicate that a row is added to CRIS_USER_LOCATION if it does not exist for that user.

The query needs to SELECT the id from CRIS_USER WHERE LocationTemp.login = CRIS_USER.login_id.

SELECT CRIS_USER.id
FROM CRIS_USER
WHERE CRIS_USER.login_id = #LocationTemp .login 

The result of that SELECT, CRIS_USER.id, is used to check if a location_id exists for a cris_user_id in the CRIS_ USER_ LOCATION table if the value for that location has an “x” in #LocationTemp.

-- If Loc_1 = “x” (or is not null) (I might use case)
SELECT *
From CRIS_ USER_ LOCATION
WHERE CRIS_USER.id(result from previous query) = CRIS_USER_LOCATION. cris_user_id and #LocationTemp. location_id = “121”

If the result of that query is null, insert a row in CRIS_USER_LOCATION. I then have to do the same for Loc_2,…etc. It would be possible to insert up to five rows into CRIS_ USER_ LOCATION from one row in #LocationTemp .

If Loc_1 = X, insert 121

If Loc_2 = X, insert 155

If Loc_3 = X, insert 063

If Loc_4 = X, insert 071

If Loc_5 = X, insert 750

There can be multiple entries for the same CRIS_USER_ LOCATION. cris_user_id since they can have multiple locations.

CRIS_USER_ LOCATION.id is the primary key and is not set to auto increment. I have to find the highest key value and increment by one when creating a new record.

Here is the code to load the text file:

IF OBJECT_ID('TEMPDB..#LocationTemp') IS NULL
	BEGIN	
	  CREATE TABLE #LocationTemp(UserName       varchar(50),UserLogin varchar(20),Loc_1 varchar(1),Loc_2 varchar(1),Loc_3 varchar(1),Loc_4 varchar(1), Loc_5 varchar(1))
      BULK INSERT #LocationTemp
      FROM '\\431605-DB1\NRVCS\UserLocation.txt'  
      WITH (ROWTERMINATOR ='\n' 
            ,FIELDTERMINATOR = '\t' 
      ,FIRSTROW=2)    
      PRINT 'File data copied to Temp table'      
END

The next step might be to load a second table #LocationTemp2 with CRIS_USER.id and Loc_1 thru Loc_5 using #LocationTemp and CRIS_USER.

IF OBJECT_ID('TEMPDB..#LocationTemp2') IS NULL
    CREATE TABLE #LocationTemp2(cris_user_id numeric(10, 0),Loc_1 varchar(1),Loc_2 varchar(1),Loc_3 varchar(1),Loc_4 varchar(1), Loc_5 varchar(1) )
    INSERT INTO #LocationTemp2(cris_user_id, Loc_1,Loc_2,Loc_3,Loc_4,Loc_5)
    SELECT CRIS_USER.id, #LocationTemp.Loc_1,#LocationTemp.Loc_2,#LocationTemp.Loc_3,#LocationTemp.Loc_4,#LocationTemp.Loc_5
    FROM   CRIS_USER JOIN #LocationTemp
       ON (CRIS_USER.login_id = #LocationTemp.UserLogin)

The next step would be to loadCRIS_ USER_ LOCATION. I would need to insert a row with the corresponding location if Loc_1 thru Loc_5 = “X”, and if that particular location is not already in the table for that cris_user_id. I’m not sure if I should do that here or with the second temp table.

I know my question is lengthy.

Any suggestions? Should I create a second temp table? Is there a better way to go about it? I’ve also thought about a SELECT within a SELECT but am unsure how to do that in combination with an INSERT. I think I would still need a case or if statement. Can I use a loop to go through Loc_1 to Loc_5 to check for “X”?

Thanks


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>