sql server - Trying to Add two sql statements and getting error with SELECT Statements -


i new sql server, having 2 select statements - , trying put union combine two. getting error message = a select statement assigns value variable must not combined data-retrieval operations.

how assign @newcomments variable ??

    declare @newcomments varchar(max)              select @newcomments      select cast(comments1 varchar(max)),cast( comments2 varchar(max)),        cast(comments3 varchar(max)),      tablecomments      commentsid = 1234     union       select top 2 @newcomments = coalesce(@newcomments,'') +      cast(comments varchar(max)) + ';'       [childtablecomments]     commentsid = 1234 

please help!!

i trying retrieve records 2 tables tablecomments , childtablecomments. tablecomments have atmax 1 record childtablecomments have many records same commentsid.

so tried top2 records childtablecomments , add them , return 1 single column can added tablecomments using union.

you can try

    declare @newcomments varchar(max);               select top 2 @newcomments = @newcomments + coalesce(comments,'')     [childtablecomments] commentsid = 1234;      select @newcomments comments, cast(comments1 varchar(max)) com1,     cast( comments2 varchar(max)) com2,     cast(comments3 varchar(max)) com3     tablecomments  commentsid = 1234 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -