SQL查询组合了数据集中的两列 [英] SQL query combine two columns from dataset

查看:100
本文介绍了SQL查询组合了数据集中的两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我创建了一个SQL查询字符串,用于从数据库中的两个不同列收集数据(列ordernr和列CM)实际上我想将列与查询中的列相结合,但我没有成功,我尝试了几种不同的方式。



这就是为什么我创建了一个数据集我把查询结果放在哪里,我的问题我还有2个不同的列。



我写了一个循环遍历第一行和第一个对象的示例循环然后下一个对象下一列相同的行然后等等..

(见下面的代码)



现在我需要一个结合了两个对象同一行成一个字符串。并将所有行放在一张桌子上。



例如:



这就是我得到的:

列ordernr栏目CM

1000这是一个示例测试



这是什么我想:



(新表名为test)



1000 ----这是一个例子测试





有人能帮帮我吗?





这里是我的代码到目前为止:



Hello everyone,

I have made a SQL Query string that gather data from two different columns in a database (column "ordernr" and column "CM") actually I would like to combine the columns to one from the query but I didn't succeed I tried a couple of different ways.

that is why I have created a data set where I put the query result, my issue I have still got 2 different columns.

I have written an example loop that loops through first row and first object then next object next column same row and then so on..
(see in code below)

now I need a function that combine the two objects same row into one string. and for all the rows put them in a table.

example:

this is what I get:
Column "ordernr" column "CM"
1000 this is an example test

this is what I want:

(new table called "test")

1000 ---- this is an example test


Could someone help me?


here is my code so far:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

       Dim da As SqlDataAdapter
       Dim SQLStr As String
       Dim cnn As SqlConnection
       DataGridView1.DataSource = Nothing
       cnn = New SqlConnection(connectionString)
       cnn.Open()

       'Query för alla kolumner
       SQLStr = "SELECT " & _
       "tt.ordernr 'Ordernr'," & _
       "PostIt.Text as 'CM'" & _
       "FROM [Teknotrans_dev].dbo.OpusOrder as tt INNER JOIN" & _
       "[MyDB].dbo.CompanyMain as c On tt.bolagsnr = c.id INNER JOIN" & _
       "[MyDB].dbo.OpusOrderrow as ord On ord.ordernr = tt.ordernr INNER JOIN" & _
       "[MyDB].dbo.PostIt as PostIt On PostIt.ordernr = tt.ordernr INNER JOIN" & _
       "[MyDB].dbo.OrderVolvoLanguageName as snSrc ON ord.kallspraknr = snSrc.spraknr INNER JOIN" & _
       "[MyDB].dbo.OrderVolvoLanguageName as snTrg ON ord.malspraknr = snTrg.spraknr"
       da = New SqlDataAdapter(SQLStr, TTCon)
       ds2 = New DataSet
       da.Fill(ds2)

       DataGridView2.DataSource = ds2.Tables(0)
       Console.WriteLine(SQLStr)

       For Each Row As DataRow In ds2.Tables(0).Rows
           For Each Coll As DataColumn In ds2.Tables(0).Columns
               Dim s As String = Row(Coll.ColumnName).ToString()

               MsgBox(s)

           Next
       Next

   End Sub







有人能帮帮我吗?





提前谢谢你




Could someone help me?


Thank you in advance

推荐答案

你必须转换ordernr varchar / char类型的字段。您可以使用如下:



SELECT转换(varchar(100),TT.ORDERNR)+''+ PostIt.Text FROM;



Ashish Nigam
You Have to Convert ordernr field in varchar/char type. You can use like:

SELECT convert(varchar(100),TT.ORDERNR) +''+ PostIt.Text FROM ;

Ashish Nigam


这篇关于SQL查询组合了数据集中的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆