Stored Procedure - Concatenate

Last post 09-04-2008 10:26 AM by preethi. 13 replies.
Page 1 of 1 (14 items)
Active Topics My Discussions Unanswered Sort Posts: Previous Next
  • 08-31-2008 9:07 AM

    Stored Procedure - Concatenate

    Hi,

    I'm trying to do a pretty simple concatenation in a stored procedure, but from some reason it doesn't work:

    please help!

    DROP PROC Temp;
    GO
    CREATE PROC Temp
    (
     @SearchStr nvarchar(100)
    )
    AS
    BEGIN
     SET NOCOUNT ON
     
     DECLARE Eli_Cursor CURSOR FOR
      Select  Name1
      From [Northwind].[dbo].[Eli];

     Declare @FileName nvarchar(40);
     Declare @tmp  nvarchar(40);
     SET  @FileName = '';
     SET  @tmp =  ''

     OPEN Eli_Cursor;

     WHILE @@FETCH_STATUS = 0
     BEGIN
      FETCH NEXT FROM Eli_Cursor INTO @FileName;

      SET @tmp = @FileName+'.jpg';

      update [Northwind].[dbo].[Eli]
      set  Name3 = @tmp
      where Name1 = @FileName;
     END
     
     CLOSE Eli_Cursor;
     DEALLOCATE Eli_Cursor;
    END
    go


     

  • 08-31-2008 11:52 AM In reply to

    • satya
    • Top 10 Contributor
    • Joined on 11-05-2002
    • United Kingdom
    • Posts 22,515
    • Microsoft MVP
      Moderator

    Re: Stored Procedure - Concatenate

    Welcome to the forums.

    What kind of error you are getting, and what is the expected result for you?

    -Satya S K J

    SQL Server MVP



    Knowledge is Power, you will gain by sharing it. SSQA.net - Invisible contributions to the users & visible success in SQL Community.
  • 08-31-2008 12:33 PM In reply to

    Re: Stored Procedure - Concatenate

    there is no error whatsoever. I just don't get the required output which is: Name3 (a field in the table) should be populated with a new value that shoud be: value of Name1+'.jpg', in this case: 'ABC.jpg'

    Name 1 and Name3 are fields in the table

     Thanks

    Eli

  • 09-01-2008 4:36 AM In reply to

    Re: Stored Procedure - Concatenate

    elireu1:

    there is no error whatsoever. I just don't get the required output which is: Name3 (a field in the table) should be populated with a new value that shoud be: value of Name1+'.jpg', in this case: 'ABC.jpg'

    Name 1 and Name3 are fields in the table

     Thanks

    Eli

    It is as simple as

    update [Northwind].[dbo].[Eli]
      set  Name3 = Name1+'.jpg'

    Madhivanan

    Failing to plan is Planning to fail
  • 09-01-2008 7:02 PM In reply to

    Re: Stored Procedure - Concatenate

    this is exactly what I did, but from some reason it didn't work. now it's working, though. I must have missed something.

     

    tnx very much

    Eli

  • 09-01-2008 11:11 PM In reply to

    • preethi
    • Top 100 Contributor
    • Joined on 07-01-2003
    • Sri Lanka
    • Posts 169

    Re: Stored Procedure - Concatenate

    Glad to hear the issue is resolved. I want to take a step back. If the value of name3 is always equal to Name1 + '.jpg', Whydo you need this column name3, and update the entire table? You can always refer it as Name1+'.jpg'. Is this something you gave as a simplified example?

    Cheers,
    Preethiviraj Kulasingham
    MCITP:DBA
  • 09-02-2008 7:46 AM In reply to

    Re: Stored Procedure - Concatenate

    it's just a simple example. the real system is way complex.

     

    thanks again.

    Eli

  • 09-03-2008 1:03 AM In reply to

    Re: Stored Procedure - Concatenate

    I think it is a cast problem. Verify the type of each columns and variables you use.

    Try this : CAST(@variable as varchar(40)) for example.

  • 09-03-2008 11:58 AM In reply to

    • preethi
    • Top 100 Contributor
    • Joined on 07-01-2003
    • Sri Lanka
    • Posts 169

    Re: Stored Procedure - Concatenate

    Just to update those who haven;t seen yet, when you specify a variable as varchar (without giving the length) SQL Server will take the default of varchar(30).

    Cheers,
    Preethiviraj Kulasingham
    MCITP:DBA
  • 09-04-2008 2:12 AM In reply to

    Re: Stored Procedure - Concatenate

    preethi:

    Just to update those who haven;t seen yet, when you specify a variable as varchar (without giving the length) SQL Server will take the default of varchar(30).

    http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx

    Madhivanan

    Failing to plan is Planning to fail
  • 09-04-2008 2:20 AM In reply to

    Re: Stored Procedure - Concatenate

    preethi:

    Just to update those who haven;t seen yet, when you specify a variable as varchar (without giving the length) SQL Server will take the default of varchar(30).

    Are you really sure about that? Wink

    DECLARE @v varchar
    SELECT @v = '123456789'
    SELECT @v

    ----
    1

    (1 row(s) affected)

    This is a frequent misreading of BOL. The default is 1. Only when you use CAST or CONVERT it defaults to 30. Bit inconsistent if you ask me, but that's the way it is. Smile

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Contributing Editor, Writer & Forum Moderator http://www.sql-server-performance.com
    Webmaster: http://www.insidesql.org
    View Frank Kalis's profile on LinkedIn

    XING
  • 09-04-2008 2:26 AM In reply to

    Re: Stored Procedure - Concatenate

    Madhivanan:

    preethi:

    Just to update those who haven;t seen yet, when you specify a variable as varchar (without giving the length) SQL Server will take the default of varchar(30).

    http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx

    Shouldn't chat while posting. There wasn't a reply when I started writing my post. Smile

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Contributing Editor, Writer & Forum Moderator http://www.sql-server-performance.com
    Webmaster: http://www.insidesql.org
    View Frank Kalis's profile on LinkedIn

    XING
  • 09-04-2008 5:43 AM In reply to

    Re: Stored Procedure - Concatenate

    FrankKalis:

    Madhivanan:

    preethi:

    Just to update those who haven;t seen yet, when you specify a variable as varchar (without giving the length) SQL Server will take the default of varchar(30).

    http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/04/column-length-and-data-length.aspx

    Shouldn't chat while posting. There wasn't a reply when I started writing my post. Smile

    Smile

    Madhivanan

    Failing to plan is Planning to fail
  • 09-04-2008 10:26 AM In reply to

    • preethi
    • Top 100 Contributor
    • Joined on 07-01-2003
    • Sri Lanka
    • Posts 169

    Re: Stored Procedure - Concatenate

    Thanks to Frank and Madivanan, who brought clarity to my post.  This is what exactly I was intended to say; As we were discussing about CASTing, I was planning to say that if we specify varchar without specifying the length {in cast operation} it will end up to a cast of varchar(30). While writing I missed the words "In cast operation".  Thank you again to both of you guys, you have taought me a good lesson on effective communication. :)

    This is the main reason I love this forum; I share what I know and I learn something what I do not know.   

    Cheers,
    Preethiviraj Kulasingham
    MCITP:DBA
Page 1 of 1 (14 items)
Active Topics   My Discussions    Unanswered Posts


© 2000 - 2007 vDerivatives Limited All Rights Reserved.