Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

SQL :: Variable declaration in queries

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
5 posts • Page 1 of 1
Author
Message
OgRo
Tux's lil' helper
Tux's lil' helper
User avatar
Posts: 108
Joined: Fri Jun 04, 2004 4:20 pm
Location: Sampa - SP - Brasil
Contact:
Contact OgRo
Website

SQL :: Variable declaration in queries

  • Quote

Post by OgRo » Wed Nov 07, 2007 7:45 pm

hello there



In my work we are using variables as the parameters for our queries in a microsoft sql server database.

There we work using queries such as:

Code: Select all


DECLARE @YEAR as INT
SET @YEAR = ?
-- the "?" thing is because we are working with Java's prepared statements

SELECT TOP 10, * FROM TABLE_NAME WHERE YEAR = @YEAR
-- the query itself comes here


The problem arrived when we decided to move our development platform from windows to linux. But there is a problem with that: our client's database will still be Microsoft SQL Server and then our code must work in both servers.



Is there a standard way (ie, ANSI SQL) for variable declaration in queries? If not, do you know another (better) way for accomplishing what we need?

By the way: using hybernate or any other mapping is not desired because we don't need object mappings. We need to receive a table-like structure. I know hybernate has it own query language and I'd love to use it, but I have no idea on how to extrat the data without mapping it to an object (I did research on this matter and I found nothing).



And please, don't RTFM me. I already did it and I couldn't find anything usefull. Also, I'm posting this here because this is the best forum about anything related to computers that I know and because we are going to use gentoo linux in our server. :D
---
OgRo

Linux user #297942
Top
dev_zero
n00b
n00b
User avatar
Posts: 28
Joined: Thu Nov 01, 2007 8:53 pm
Location: Somewhere in the kernel source

  • Quote

Post by dev_zero » Thu Nov 08, 2007 4:13 am

I'm not savvy in sql at all, but i believe microsoft never uses any form for standards, so the syntax will not be the same. However coudn't you move to mysql? I think the mysql version of the above would be something like:

Code: Select all

DECLARE YEAR INT
SET YEAR = ?
SELECT * FROM TABLE_NAME WHERE YEAR = YEAR LIMIT 10
But i guess this will be too much work to convert to a different sql server :?
Dell Latitude D630 DSDT
Top
timeBandit
Bodhisattva
Bodhisattva
User avatar
Posts: 2719
Joined: Fri Dec 31, 2004 1:54 am
Location: here, there or in transit

  • Quote

Post by timeBandit » Thu Nov 08, 2007 4:44 am

Speaking from long experience, I can promise you the only way to maintain portable SQL over the long term is to test on every platform where you need to run. There's no reason not to develop on Linux end-to-end (including the database server), but if your clients run SQL Server on Windows it's unwise (foolish, really) not to keep a SQL Server machine in-house. No one wants the client to be the first to see portability bugs (unless gaining a reputation for mediocrity is in the business plan :wink:).

You're using JDBC, right? Regarding your example, what does the SQL variable give you over a simple replaceable parameter? Such as:

Code: Select all

SELECT * FROM TABLE_NAME WHERE YEAR = ?
If you set the variable so that it can be referenced multiple times in the query, while only setting a single replaceable parameter...sorry, you'll have to sacrifice that to portability. Use a JDBC parameter in the prepared statement everywhere the SQL variable appeared and set the same value into each occurrence.

As for the row return limit, I don't know a portable way to achieve that in SQL. To do that in my experience we always count rows and close the result set at the limit.
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Top
OgRo
Tux's lil' helper
Tux's lil' helper
User avatar
Posts: 108
Joined: Fri Jun 04, 2004 4:20 pm
Location: Sampa - SP - Brasil
Contact:
Contact OgRo
Website

  • Quote

Post by OgRo » Thu Nov 08, 2007 5:08 pm

Thanks for your answers.


dev_zero: In fact, the way variables work in MySQL is just like in SQL Server, except for the DECLARE part (which is not allowed in the Query context in MySQL).

timeBandit: you are right, it's not advisable not to maintain the same server the client has in your production environment, but using some strong and well tested standard should be enough to ensure it's working fine.

We could also develop using MySQL but still have a SQL Server for testing. This way we could purchase per user license for only 1 user in SQL Server and we'd have a way for making sure our product is compatible with the client's infrastructure.
---
OgRo

Linux user #297942
Top
timeBandit
Bodhisattva
Bodhisattva
User avatar
Posts: 2719
Joined: Fri Dec 31, 2004 1:54 am
Location: here, there or in transit

  • Quote

Post by timeBandit » Thu Nov 08, 2007 6:28 pm

OgRo wrote:[S]ome strong and well tested standard should be enough to ensure it's working fine. We could also develop using MySQL but still have a SQL Server for testing. This way we could ... [be] sure our product is compatible with the client's infrastructure.
Exactly, that's all I'm advising. Besides the small differences between the database vendors' SQL dialects, the quality and capabilities of their JDBC drivers is a factor. Even if you and I conform to standards, we must be aware of where the vendors didn't :roll:, if we expect our products to work.
Plants are pithy, brooks tend to babble--I'm content to lie between them.
Super-short f.g.o checklist: Search first, strip comments, mark solved, help others.
Top
Post Reply

5 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic