开发者

SQL concatenation inside VBA string

I'm using VBA excel 2003,SQL 2005 to make a sql query call and inside my sql statement I'm using '+' operator to concatenate two strings.

dim query as string
query =  "Select distinct ', '+emailaddress1 "
query = query & "from contact "

would this work inside vba? My query returns too many records in excel but not in SQL?

Please just focu开发者_如何学JAVAs on this 2 lines of code and not worry about the rest of my sql call, I'm just wondering whether or not this specific string would work?


Your code will return a column where each row would be an email address with a comma in front of it. If that is what you want, then yes, it will work.

If, on contrary, you want a single string where all email addresses would be listed, separated with commas, that'd be

query =         "declare @foo varchar(max);"
query = query & "select distinct @foo = isnull(@foo,'') + emailaddress1 + ', ' from contact;"
query = query & "select left(@foo, len(@foo)-2);"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜