简体   繁体   English

SQL:我想从第二个表中提取数据。 由于第二个表的列名与第一个表列的逗号分隔的值匹配

[英]SQL: I want to extract data from second table. As column names of second table matches with values Separated by comma of first table column

I have a table1 with one column with values seperated by comma firstname,lastname .我有一个 table1,其中一列的值由逗号firstname,lastname分隔。 I have a table2 with two column names firstname and lastname , which match with table1's single column values( firstname,lastname ).我有一个 table2 有两个列名firstname 和 lastname ,它们与 table1 的单列值( firstname,lastname )匹配。 My necessity is I want to extract data from table2 by using select statement SELECT firstname,lastname FROM table2 .我的必要性是我想通过使用选择语句SELECT firstname,lastname FROM table2从 table2 中提取数据。 I used variables to write query.我使用变量来编写查询。

I transferred values of table1( firstname, lastname ) to @link variable and this operation completed successfully.我将 table1( firstname, lastname ) 的值传输到@link变量,此操作成功完成。 But when i tried to add SELECT @link FROM table2 to the query i thought i would get data of firstname and lastname of table2, But result showing a seperate no column name and value in that column as firstname,lastname .但是当我尝试将SELECT @link FROM table2添加到查询中时,我想我会得到 table2 的名字和姓氏的数据,但是结果显示该列中没有单独的列名和值作为firstname,lastname

Tons of Thanks for any help!非常感谢您的帮助!

declare @link nvarchar(max)
    select @link = (select [column]
    from [dbo].[table1])
    from [dbo].[table2]
    SELECT @link FROM table2 

You appear to be attempting "dynamic sql", ie you build a string that contains a select statment, then execute that string as a query.您似乎正在尝试“动态 sql”,即您构建一个包含选择语句的字符串,然后将该字符串作为查询执行。 Conventionally you will find many example that use @sql for this, and the basic fom of this is as follows:按照惯例,您会发现很多使用@sql的示例,其基本形式如下:

declare @sql nvarchar(max)

select @sql = N'select [column] from [dbo].[table1]'

EXEC sp_executesql @sql;

Note that the @sql contents has to be valid sql syntax.请注意,@sql 内容必须是有效的 sql 语法。 On this point your code contains 2 from clauses and that is NOT valid, plus it isn't clear what you intended, perhaps it was meant to be a join.在这一点上,您的代码包含 2 个from子句,这是无效的,而且不清楚您的意图是什么,也许它本来是一个连接。

declare @sql nvarchar(max)

select @sql = N'select t.[column], j.[column2]
from [dbo].[table1] as t
inner join [dbo].[table2] as j on t.id = j.fk'

EXEC sp_executesql @sql;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当第一个表中的列值与第二个表中的列值匹配时,显示一个表中的数据 - Display data from one table when a column value in first table matches column value in second table SQL查询从一个表中提取与第二个表中的单列数据匹配的数据 - SQL query to pull data from one table that matches single column data from second table 如果第一个表中的列中的值,SQL Server从第二个表返回数据 - SQL Server return data from second table if value in column in first table 要在用逗号分隔的SQL表的列中插入多个值, - Want to insert multiple value in column of SQL table separated by Comma , SQL-基于一个表与另一个表的联接(第一个表的行值到第二个表的列值) - SQL - Joining one table with another based on ( Row value from first table to Column value of second table) 当 ID 匹配时,我需要将第二个表中的值插入到第一个表中 - I need to insert the values from the second table to the first table whenewer the ID matches 使用条件从sql表转换某些用逗号分隔的字符串的列名 - convert certain column names with comma separated string from sql table with conditions 将逗号分隔值转换为多列表 - convert comma separated values into multi column table 每次将数据插入分支表时,我都想创建一个表。 但名称应来自分支表 ifsccode 列 - Everytime data is inserted in branch table i want to create a table. but the name should come from branch table ifsccode column 从逗号分隔列表中的联接表中获取多个列值 - Get multiple column values from joined table in a comma separated list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM