Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 基于表之间的比较获取列的名称_Sql_Sql Server 2008_Excel - Fatal编程技术网

Sql 基于表之间的比较获取列的名称

Sql 基于表之间的比较获取列的名称,sql,sql-server-2008,excel,Sql,Sql Server 2008,Excel,我的目标是根据表Product_a和Product_B中的相似列检索列名称的结果 无论我是在excel、记事本还是sql server的表格中获得结果。最重要的是检索结果 我不知道如何在人工使用率低的情况下根据计算结果检索结果 您需要考虑的一些标准: 列必须具有相同的数据类型和名称才能相似 我不想从列中检索数据 该表是在基本级别中创建的。实际上,该表使用200列。 该表位于SQL server 2008 R2中 TABLE Product_A ( ProductID INT, Pro

我的目标是根据表Product_a和Product_B中的相似列检索列名称的结果

无论我是在excel、记事本还是sql server的表格中获得结果。最重要的是检索结果

我不知道如何在人工使用率低的情况下根据计算结果检索结果

您需要考虑的一些标准:

列必须具有相同的数据类型和名称才能相似 我不想从列中检索数据 该表是在基本级别中创建的。实际上,该表使用200列。 该表位于SQL server 2008 R2中

TABLE Product_A
(
   ProductID INT,
   ProductName VARCHAR(100),
   Rate MONEY,
   Action INT,
   MapINT,
) 


TABLE Product_B
(
   ProductID INT,
   Count INT,
   ProductName VARCHAR(100),
   Rate MONEY
) 



Requested result

       ProductID INT,
       ProductName VARCHAR(100),
       Rate MONEY,

您的要求不是很清楚,但您可能指的是一个联合,因此您可以从两个表中获取数据,这将在单独的行中为您提供每个表中的产品信息:

SELECT ProductId, ProductName, Rate
FROM Product_A
UNION 
SELECT ProductId, ProductName, Rate
FROM Product_B
如果您可以编辑OP并发布一些示例数据和预期结果,那么查询就可以得到澄清

如果您知道要从表A或表B中获取数据的表,则可以在ProductName上执行联接,但需要确定所需的速率和产品Id:

SELECT a.ProductId
    , a.ProductName
    , a.Rate
FROM Product_A a
JOIN Product_B b
    ON a.ProductName = b.ProductName

此查询将比较两个表,并仅返回相同数据类型的相同命名列。若需要查找accross数据库中的所有匹配项,请注释掉t1.name=condition

select 
      t1.name TableName, 
      c1.name ColumnName,
      types.name,
      c1.max_length,
      c1.precision,
      c1.scale
 from sys.columns c1
inner join sys.columns c2
   -- Condition to join columns - same name, type, length, precision and scale
   on c1.name = c2.name
  and c1.system_type_id = c2.system_type_id
  and c1.max_length = c2.max_length
  and c1.precision = c2.precision
  and c1.scale = c2.scale
   -- Exclude template table
  and c1.object_id <> c2.object_id
inner join sys.tables t1
   on c1.object_id = t1.object_id
inner join sys.types
   on c1.system_type_id = types.system_type_id
inner join sys.tables t2
   on c2.object_id = t2.object_id
where t2.name = 'Product_B'
   -- remove if you want to search for matches in all tables 
  and t1.name = 'Product_A'

查看和上的文档。

您可以发布一些示例数据和预期结果吗?您想要两个表中的产品吗?如果费率不同,您想要哪种费率?