0

I need some knowledge or guidance on T-SQL coding.

I want to build a stored procedure which will do a merge transaction by taking source and target tables from its parameters. I also want to use source and target column names dynamically inside the stored procedure.

Your help is highly appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user15488631
  • 85
  • 2
  • 10
  • You'll need DynamicSQL, write your query and concatenate in the parameter values, and execute with `exec sp_executesql` – Stu Jun 14 '21 at 16:16
  • Hi Stu, do you have any article/blogs which can be used for me as reference guide. – user15488631 Jun 14 '21 at 16:22
  • I just searched on StackOverflow and [found this](https://dba.stackexchange.com/questions/212815/parameterize-table-name-in-dynamic-sql) – Stu Jun 14 '21 at 16:24
  • HI Stu, can you help me how to convert rows into columns in sql. For example if i run the query SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'sampletable' Now i want to put the values as column names in a dynamic select statement. can you suggest me how to do it. – user15488631 Jun 14 '21 at 17:05
  • Stack Overflow is not a code-writing service; have a go, show us what you can do and if necessary ask for help about a *specific* problem you cannot resolve. – Stu Jun 14 '21 at 17:30
  • You can find a good start on this here: https://stackoverflow.com/a/1246848/109122. However, be forewarned, doing this for a MERGE is a lot more complicated than for a SELECT. Also, pay close attention to the security warnings there. – RBarryYoung Jun 14 '21 at 17:31
  • This approach has a bad smell even if it works. I suggest you read about [coupling and cohesion](https://www.geeksforgeeks.org/software-engineering-coupling-and-cohesion/) first. And a module that will attempt to merge any two tables will require significant skill in writing tsql and handling errors. – SMor Jun 14 '21 at 17:36

0 Answers0