Print only required fileds from a table

CREATE TABLE Persons
 (
 CustomerName varchar(255),
 ContactName varchar(255),
 Address varchar(255),
 City varchar(255)
 ); 
 

 
 INSERT INTO Persons (CustomerName, ContactName, Address, City)
 VALUES ('David','Matt','Drive through 96','Mille');
  INSERT INTO Persons (CustomerName, ContactName, Address, City)
  VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger');

from this table i want to print only customer name and concatanation of  Address and city. what is the best way to do.

expected output

David   Drive through 96 Mille
Cardinal Skagen 21 Stavanger

March 25th, 2015 3:16pm

... print? Do you mean select?

SELECT customerName, Address+city
  FROM persons

I'm beginning to think you're taking a course and having us do your homework for you..

Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 3:23pm

No using Print statement..

i want output in this format

March 25th, 2015 3:28pm

Then you'll want to use a cursor:

DECLARE @customerName VARCHAR(255), @addressCity VARCHAR(510)

DECLARE c1 CURSOR FOR
SELECT customerName, Address+city
  FROM persons
OPEN c1
FETCH c1 INTO @customerName, @addressCity
WHILE @@FETCH_STATUS <> -1
BEGIN
  PRINT @customerName + ' '+ @addressCity
FETCH c1 INTO  @customerName, @addressCity
END
CLOSE c1
DEALLOCATE c1

You didn't answer my question... are you doing a course and coming here for answers?

Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 3:49pm

With cursor i am already getting the output, i was looking for some alternative.

The customers are not interested in using cursor.

The answer to your question:

No sir, i m not doing any course. I am new to stored procedure and i have finish my job so i take help of you people and google.

March 25th, 2015 3:53pm

You could do it this way too... but it's a poor idea.

DECLARE @gertBig VARCHAR(MAX) = ''

SELECT @gertBig = @gertBig + customerName + ' ' + address+city + CHAR(10)
  FROM Persons

PRINT @gertBig

Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 3:58pm

Ok. But i heard using cursor is not a good thing to use and temp tables gives better performance

I got the below error.

March 25th, 2015 4:08pm

It's because the PRINT isn't meant for that - this is a job for a normal SELECT statement (as per the second post)

Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 4:29pm

Thanks a lot

this one has worked

DECLARE

@gertBig VARCHAR(1000)


set

@gertBig = ''



SELECT

@gertBig = @gertBig + customerName + ' ' + address+city + CHAR(10)


 

FROM Persons



PRINT

@gertBig

March 25th, 2015 4:30pm

You're using an old version of SQL server that doesn't support default values on local variables, that's why it didn't work.
Free Windows Admin Tool Kit Click here and download it now
March 25th, 2015 5:26pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics