0

I have a stored procedure which is returning few columns (now this stored procedure is being managed by other teams so they can add further columns in it)

ALTER SP MyCallingSP
AS
    SELECT col1, col2, col3, col4 
    FROM Table
END

I am calling this stored procedure from my C# code in which I am inserting the result set into a data table.

Now, I have to create a global temporary table from this datatable. Problem is, tomorrow they can add a new column in it and then it might fail my process.

From the above example, I just need col1, col2, col3 which I want to insert into a global temporary table.

I am able to get the data into datatable but I am not sure about how to proceed further.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206
  • Why would it fail your process? Just use the columns you need in the data table – Charlieface Aug 02 '21 at 09:27
  • I have filled datatable using data adapter. So, my datatable has all 4 columns. So, how can I now create temp table for only 3 columns out if it? – Zerotoinfinity Aug 02 '21 at 09:30
  • Why do you want to, why do you care how many columns are in the datatable? WHat process do you have that actually cares what extra columns there are? – Charlieface Aug 02 '21 at 09:31
  • I am feeling dumb now :( I don't know what to say. Are you saying I can insert only required columns from datatable to temporary table? Or please correct me if I my understanding is wrong – Zerotoinfinity Aug 02 '21 at 09:33
  • Start at the beginning: what are you trying to *do*, what is the global temp table for? And have you considered using `SqlBulkCopy` to insert the columns you need? – Charlieface Aug 02 '21 at 09:35
  • I have insert the results from the stored procedure into a global temporary table. So, right now SP has 3 columns so I can manually loop datatable which got filled from the SP and loop through it. write CREATE ##MyTable TABLE with column name. – Zerotoinfinity Aug 02 '21 at 09:37
  • I am checking bulkcopy now, thanks for the suggestion. Is this code is doing the same? Calling SP and then dumping only required columns to temp table? https://stackoverflow.com/questions/34839404/select-into-temptable-from-c-sharp-datatable – Zerotoinfinity Aug 02 '21 at 09:38
  • You need to add `ColumnMapping`s and that code is also missing `using` statements – Charlieface Aug 02 '21 at 09:40

0 Answers0