YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

Stored procedure syntax Options · View
jhannah
#1 Posted : Sunday, February 28, 2010 9:18:21 AM
Rank: Newbie

Groups: Registered

Joined: 2/28/2010
Posts: 3
Location: Omaha, NE
Howdy,

Is there a mailing list, or just this forum?

Below, some syntax I'm wrestling with. There's got to be a better way...?

''''''')' is really hard to debug, and then MS-SQL locks up when I do something wrong.

Is there a way to declare an SQL variable type or something so I don't have to try to stringify all the SQL before hand?

Thanks,

j



declare @BeginDate datetime
declare @EndDate datetime
declare @strsql varchar(max)
declare @OPENQUERY varchar(max)
declare @LinkedServer varchar(20)

set @LinkedServer = 'MS_OPERA'

set @BeginDate = convert(char(11),getdate() - 3,120)
set @EndDate = convert(char(11),getdate() - 3,120)

--pull primary reservation data from reservation_general view
set @strsql = 'SELECT RESV_NAME_ID, cast(GUEST_NAME_ID as varchar2(25)) as GUEST_NAME_ID, PHONE_ID, ADDRESS_ID, MEMBERSHIP_ID, GUEST_FIRST_NAME,
GUEST_MIDDLE_NAME, GUEST_NAME, GUEST_COUNTRY, GUEST_PHONE, RESERVATION_DATE, RESORT, CONFIRMATION_NO,
MARKET_CODE, MARKET_DESC, RATE_CODE, NIGHTS, SHARE_AMOUNT, EFFECTIVE_RATE_AMOUNT,MEMBERSHIP_TYPE,
RESV_STATUS, ROOM_CATEGORY_LABEL, ADULTS, CHILDREN, ARRIVAL, DEPARTURE, UPDATE_DATE, PROMOTIONS
FROM RESERVATION_GENERAL_VIEW WHERE RESV_STATUS = ''''CHECKED OUT'''' AND DEPARTURE BETWEEN ''''' + convert(char(11),@BeginDate,13) + '''''
and ''''' + convert(char(11),@EndDate,13) + ''''''')'

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[OPERA_RES_GEN]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[OPERA_RES_GEN]

set @OPENQUERY = 'SELECT *
into OPERA_RES_GEN from openquery(' + @LinkedServer + ','''
exec(@OPENQUERY + @strsql)
mruwe
#2 Posted : Sunday, February 28, 2010 10:19:56 AM
Rank: Administration

Groups: Administrators , DownloadAdmin, EventAdmin, JobAdmin, Leaders, Registered

Joined: 1/24/2009
Posts: 8
Location: Omaha, NE
Hi,

I'll make sure to forward your post to one of our SQL leaders who might be able to help out.

Thanks,
Matt
jmorehouse
#3 Posted : Tuesday, March 02, 2010 7:11:48 AM
Rank: Administration

Groups: Leaders , Registered

Joined: 5/13/2009
Posts: 3
Hello,

If you are using SQL 2008, you can reduce set the values of the parameters in the same line in which you delcare them. IE:

declare @BeginDate datetime = convert(char(11),getdate() - 3,120)
declare @EndDate datetime = convert(char(11),getdate() - 3,120)
declare @LinkedServer varchar(20)= 'MS_OPERA'
declare @strsql varchar(max), @OPENQUERY varchar(max)

You can also declare multiple parameters on the same line. Just helps clean things up. If your not using SQL 2008, you'll have to keep it the way you have it.


--pull primary reservation data from reservation_general view
-- There were too many ''''''' at the end of the statement. I've reduced them to what they needed to be (I think)
set @strsql = 'SELECT RESV_NAME_ID, cast(GUEST_NAME_ID as varchar2(25)) as GUEST_NAME_ID, PHONE_ID, ADDRESS_ID, MEMBERSHIP_ID, GUEST_FIRST_NAME,
GUEST_MIDDLE_NAME, GUEST_NAME, GUEST_COUNTRY, GUEST_PHONE, RESERVATION_DATE, RESORT, CONFIRMATION_NO,
MARKET_CODE, MARKET_DESC, RATE_CODE, NIGHTS, SHARE_AMOUNT, EFFECTIVE_RATE_AMOUNT,MEMBERSHIP_TYPE,
RESV_STATUS, ROOM_CATEGORY_LABEL, ADULTS, CHILDREN, ARRIVAL, DEPARTURE, UPDATE_DATE, PROMOTIONS
FROM RESERVATION_GENERAL_VIEW WHERE RESV_STATUS = ''CHECKED OUT'' AND DEPARTURE BETWEEN ''' + convert(char(11),@BeginDate,13) + '''
and ''' + convert(char(11),@EndDate,13) + ''')'

-- I changed this from a select * to a select 1, which really is just true or false. This is just a coding preference in my opinion. I also wrapped the drop statement with a BEGIN/END
IF EXISTS (select 1 from dbo.sysobjects where id = object_id(N'[dbo].[OPERA_RES_GEN]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
DROP TABLE [dbo].[OPERA_RES_GEN]
END

set @OPENQUERY = 'SELECT *
into OPERA_RES_GEN from openquery(' + @LinkedServer + ','''
--exec(@OPENQUERY + @strsql)

-- An easy way to debug these is to just print the statement out as I did below. Once you've validated that the code is producing the correct results, then you can remove the comment block from the EXEC statement and away you go.
PRINT (@OPENQUERY + @strsql)

-- Is SQL locking up or just running for a long time? I don't know what kind of data your pulling across, but if it's a large data set, it might take some time.


Hope this helps!!

John Morehouse
Omaha SQL/BI User Group Leader
jhannah
#4 Posted : Tuesday, March 02, 2010 5:09:42 PM
Rank: Newbie

Groups: Registered

Joined: 2/28/2010
Posts: 3
Location: Omaha, NE
jmorehouse wrote:

If you are using SQL 2008


Eeek... I'm back on SQL Server 9.0.4035 (2005 SP3 apparently). I'll keep your advice in mind when we upgrade (whenever corporate does that...).

Thanks. Hopefully I can leverage that some day. For now, I'll just continue drowning in single quotes. :)

j
ehaley
#5 Posted : Friday, March 05, 2010 10:52:56 AM
Rank: Newbie

Groups: Registered

Joined: 3/5/2010
Posts: 2
Location: La Vista, NE
Here's what I came up with:

First off, please understand that I can be anal when it comes to SQL optimization and formatting. Secondly, I am by no means a pro, and I don't play one on TV, but, I did stay at a Holiday Inn Express last night...

Here are a couple of notes regarding the way I addressed this SQL Server 2005 query in a short period of time (I would have done more, but I am somewhat limited on time):
1. Variable declarations can be combined into a single comma-separated statement.
2. Variables can be initialized in a single comma-separated statement.
3. The @BeginDate and @EndDate variables are converted twice, when, in this specific case, it can actually be done once.
4. Whenever possible, I prefer to use variables instead of inline calculations when creating sql statement strings.

Anyway, this is just my personal take on it. I hope this helps.

BTW, I used SQL Pretty Printer (www.wangz.net) for the code formatting, in case anyone was wondering.


Code:

DECLARE @BeginDate    DATETIME,
        @EndDate      DATETIME,
        @StrSql       VARCHAR(MAX),
        @OpenQuery    VARCHAR(MAX),
        @LinkedServer VARCHAR(20);

SELECT @LinkedServer = 'MS_OPERA',
       @BeginDate = CONVERT(CHAR(11), GETDATE() - 3, 106),
       @EndDate = CONVERT(CHAR(11), GETDATE() - 3, 106),
       -- Pull primary reservation data from reservation_general view
       @StrSql = 'SELECT RESV_NAME_ID,
                         CAST(GUEST_NAME_ID AS VARCHAR2(25)) AS GUEST_NAME_ID,
                         PHONE_ID,
                         ADDRESS_ID,
                         MEMBERSHIP_ID,
                         GUEST_FIRST_NAME,
                         GUEST_MIDDLE_NAME,
                         GUEST_NAME,
                         GUEST_COUNTRY,
                         GUEST_PHONE,
                         RESERVATION_DATE,
                         RESORT,
                         CONFIRMATION_NO,
                         MARKET_CODE,
                         MARKET_DESC,
                         RATE_CODE,
                         NIGHTS,
                         SHARE_AMOUNT,
                         EFFECTIVE_RATE_AMOUNT,
                         MEMBERSHIP_TYPE,
                         RESV_STATUS,
                         ROOM_CATEGORY_LABEL,
                         ADULTS,
                         CHILDREN,
                         ARRIVAL,
                         DEPARTURE,
                         UPDATE_DATE,
                         PROMOTIONS
                  FROM   RESERVATION_GENERAL_VIEW
                  WHERE  RESV_STATUS = ''CHECKED OUT''
                     AND DEPARTURE BETWEEN ''' + @BeginDate + ''' AND ''' + @EndDate + '''';

IF EXISTS (SELECT *
           FROM   dbo.sysobjects
           WHERE  id = object_id(N'[dbo].[OPERA_RES_GEN]')
              AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
    DROP TABLE [dbo].[OPERA_RES_GEN];

SET @OpenQuery = 'SELECT *
                  INTO   OPERA_RES_GEN
                  FROM   OPENQUERY(' + @LinkedServer + ', '')';

EXEC(@OpenQuery + @StrSql);

jhannah
#6 Posted : Friday, March 05, 2010 10:58:59 AM
Rank: Newbie

Groups: Registered

Joined: 2/28/2010
Posts: 3
Location: Omaha, NE
ehaley wrote:
Here's what I came up with:


Thanks! I'll see if that runs on our server when I'm back in that office Tuesday.

j
ehaley
#7 Posted : Friday, March 05, 2010 11:00:24 AM
Rank: Newbie

Groups: Registered

Joined: 3/5/2010
Posts: 2
Location: La Vista, NE
jhannah wrote:
ehaley wrote:
Here's what I came up with:


Thanks! I'll see if that runs on our server when I'm back in that office Tuesday.

j


Anytime!
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by YAF 1.9.3 RC2 | YAF © 2003-2008, Yet Another Forum.NET
This page was generated in 0.250 seconds.