question

MadhusudanReddyMandala-4641 avatar image
0 Votes"
MadhusudanReddyMandala-4641 asked EchoLiu-msft edited

How to Select Row by Row from a table

I have a table in that table has 1 column(Cols) and it has 2 Records see below


125154-ta1.png


I have another table and it has 4 columns see below

125177-00.png


I want to insert first table 2 columns into 2nd table FieldName column


How to do that?


sql-server-generalsql-server-transact-sqlsql-server-reporting-services
ta1.png (924 B)
00.png (1.3 KiB)
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your question is anything but clear. For these type of questions, we recommend that you post CREATE TABLE statements for your tables and INSERT statements with test data, and the expected result of the test data. The test data set should preferably be big enough to illustrate all angles of the problem. This makes it easy to copy and paste into a query window to develop a tested solution.

It also helps if you give a short description of the business rules that explains why you want that particular result.

Finally, include the output from "SELECT @@version".

0 Votes 0 ·

My Question is

For example i have two tables table-1 and table-2.

I have created a Dynamic Stored procedure with 2 parameters that takes two table names.
.
That stored procedure code copies the whatever i added or deleted rows in table-1 it will add all those column names into table-2

If i add or delete column(s) in table-1 after that i execute that Stored procedure it will add or delete the rows into table-2.

Now i want to record all activities like
TableName(Which table is effected after execute Stored procedure),
FieldName(Which is nothing but column name of added in table-2),
OperationType(Added/Deleted message),
Date(Date of execution).


This is the problem

0 Votes 0 ·
ErlandSommarskog avatar image ErlandSommarskog MadhusudanReddyMandala-4641 ·

I understand even less now. As a starting point, could you post a script with that stored procedure an example of it in action, so that we understand what is is doing?

I would also be curious with the ultimate purpose of this procedure. Passing table names as parameter isn't exactly a vanilla operation.

0 Votes 0 ·
Show more comments
Viorel-1 avatar image
1 Vote"
Viorel-1 answered

Maybe you need something like this:

 insert AnotherTable (TableName, FieldName, OperationType, [Date])
 select @FinalTable, Cols, 'Added', getutcdate()
 from #AddedColumns
 union all
 select @FinalTable, Cols, 'Deleted', getutcdate()
 from #DeletedColumns


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

EchoLiu-msft avatar image
0 Votes"
EchoLiu-msft answered EchoLiu-msft edited

Hi @MadhusudanReddyMandala-4641,

Welcome to the microsoft TSQL Q&A forum!

Could you please validate all the answers so far and provide any update?
If all of them are not working or helpful, please provide more sample data or details about your issue.
Please remember to accept the answers if they helped. Your action would be helpful to other users who encounter the same issue and read this thread.

Thank you for understanding!

Regards
Echo

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.