1

I am working on a project that has a C# front end that will be used to select a file for importing into an MSSQL SQL Database. In the table there will be an additional column called 'recommendedAction' (tinyint - 0-5 only)

I would like to have sql fill in the 'recommendedAction' column based on criteria in a different table.

Is there a way that when SQL is importing (SSIS or pure TSQL) it could read the values of a table and fill in the 'action' based on the criteria? Or is this something that should be done in the C# frontend?

EDIT

SQL table structure for imported data (with additional column)

Create Table ImportedData (
Column1 INT Identity,
Column2 VARCHAR(10) NOT NULL,
Column3 CHAR(6) NOT NULL,
RecommendedAction TINYINT NOT NULL
)

Table structure of recommended action criteria

Create Table RecommendedActions(
ID INT Identity,
ActionID TINYINT NOT NULL, --value to put in the RecommendedAction column if criteria is a match
CriteriaColumn VARCHAR(255) NOT NULL --Criteria to match against the records
)

Example records for RecommendedActions

ID    ActionID    CriteriaColumn
1     2           'Column2 LIKE ''6%'''
2     3           'Column2 LIKE ''4%'''

Now when a new set of data is imported, if Column2 has a value of '6032' it would fill in a RecommendedAction of 2

3
  • Please provide sample data and desired results. Explain the logic for assigning values from the other table as well. Commented May 3, 2021 at 17:30
  • 1
    Does this answer your question? How to Update Table with Using Inner Join and Case Statement Commented May 3, 2021 at 17:36
  • How varied are your criteria, can they all be defined by column operator value? Commented May 3, 2021 at 20:59

3 Answers 3

0

Many ways exist. For example you can insert into the tb table a value selected from the ta table according to criteria.

Example setup

create table ta(
 Id int,   
 val int);

insert into ta(ID, val) values
   (1, 30)
  ,(2, 29)
  ,(3, 28)
  ,(4, 27)
  ,(5, 26);
  
create table tb
(Id int,   
ref int);

Example insert

-- parameters
declare @p1 int = 1, 
        @p2 int = 27;

-- parameterized INSERT
insert tb(Id, ref)
   values(@p1, (select ta.id from ta where ta.val=@p2));
Sign up to request clarification or add additional context in comments.

Comments

0

Below added Stored procedure will do the job. It gets the Action column value based on the Column2 parameter and insert into the ImportedData table. You can execute this Stored procedure inside the C# code with required parameters. I added sample execute statements for to test the query.

Sample data inserted to the RecommendedActions Table:

INSERT INTO  RecommendedActions  
VALUES 
 (2, 'Column2 LIKE ''6%''')
 ,(3, 'Column2 LIKE ''4%''')

Stored Procedure Implementation : CREATE PROCEDURE Insert_ImportedData(

  @Column2  AS VARCHAR(10)
 ,@Column3 AS CHAR(3)
 )
 AS 
  BEGIN

    DECLARE @RecommendedAction AS TINYINT   

     SELECT @RecommendedAction = ActionID 
     FROM   RecommendedActions 
     WHERE SUBSTRING(CriteriaColumn, 15, 1) = LEFT(@Column2 , 1)    

     INSERT INTO ImportedData VALUES (@Column2,@Column3,@RecommendedAction)


   END
GO

This is the execute statement for the Above Stored procedure

  EXEC  Insert_ImportedData '43258' , 'ATT'
  EXEC  Insert_ImportedData '63258' , 'AOT'

Output after SP execution

Comments

0

you can use sqlalchemy in python and load your data into a dataframe then append the dataframe to the sql table. You can set the dtype for each of the field datatype in the read_csv using a dictionary. Loading data with Python is super powerful because the bulk load is fast. Use your c# code to build the csv file using stream io and use linq to for your conditions for data fields. Then use python to load your csv.

import pandas as pd
from sqlalchemy import create_engine
engine = create_engine(connectionstring)

df = pd.read_csv("your_data.csv", header=None)
df.columns = ['field1', 'field2', 'field3']
df.to_sql(name="my_sql_table", con=connection,  if_exists='append', index=False)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.