473,394 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

stored procedure varaiables

Hi everyone,

I'm learning T-SQL and I have created a copy of the pubs database called
pubs2.

I both the original pubs database and pubs2 I have created an identical view
called TableFields.

In the database in which it resides this view retrieves: Every table in the
database as WS_TABLE, and Every field in the database as WS_FIELD (it also
retrieves the datatype and length of the field but that isn't important
right now).

I have been creating and dropping tables in pubs2 and also adding and
deleting fields.

I have written a SQL statement that compares the two databases and lists the
tables that are in pubs but not in pubs2

SELECT DISTINCT DB1.WS_TABLE, DB2.WS_TABLE
FROM pubs..TableFields DB1 LEFT JOIN pubs2..TableFields DB2 ON DB1.WS_TABLE
= DB2.WS_TABLE
WHERE DB2.WS_FIELD IS NULL

I would like to turn this statement into a stored procedure called
sfp_DB_Table_Compare which accepts the databases to be compared as
variables.

Ideally the syntax would be something like:

EXECUTE sfp_DB_Table_Compare pubs, pubs2

My failed attempt is as follows: -

CREATE PROCEDURE sfp_Db_Table_Compare
(@DBase1 varchar(20), @DBase2 varchar(20))
SELECT DISTINCT DB1.WS_TABLE, DB2.WS_TABLE
FROM DB1..TableFields DB1 LEFT JOIN DB2..TableFields DB2 ON DB1.WS_TABLE =
DB2.WS_TABLE
WHERE DB1 = @DBase1
AND DB2 = @Dbase2
AND DB2.WS_FIELD IS NULL
RETURN

Can anybody help me please?


Jul 23 '05 #1
2 1230
Hi

You would have to resort to dynamic SQL to do this as you can't use a
variable as the database name.
Check out http://www.sommarskog.se/dynamic_sql.html for a discussion on
using dynamic SQL.

John

"Stevie D" <St***@127.0.0.1> wrote in message
news:d1**********@news7.svr.pol.co.uk...
Hi everyone,

I'm learning T-SQL and I have created a copy of the pubs database called
pubs2.

I both the original pubs database and pubs2 I have created an identical
view
called TableFields.

In the database in which it resides this view retrieves: Every table in
the
database as WS_TABLE, and Every field in the database as WS_FIELD (it also
retrieves the datatype and length of the field but that isn't important
right now).

I have been creating and dropping tables in pubs2 and also adding and
deleting fields.

I have written a SQL statement that compares the two databases and lists
the
tables that are in pubs but not in pubs2

SELECT DISTINCT DB1.WS_TABLE, DB2.WS_TABLE
FROM pubs..TableFields DB1 LEFT JOIN pubs2..TableFields DB2 ON
DB1.WS_TABLE
= DB2.WS_TABLE
WHERE DB2.WS_FIELD IS NULL

I would like to turn this statement into a stored procedure called
sfp_DB_Table_Compare which accepts the databases to be compared as
variables.

Ideally the syntax would be something like:

EXECUTE sfp_DB_Table_Compare pubs, pubs2

My failed attempt is as follows: -

CREATE PROCEDURE sfp_Db_Table_Compare
(@DBase1 varchar(20), @DBase2 varchar(20))
SELECT DISTINCT DB1.WS_TABLE, DB2.WS_TABLE
FROM DB1..TableFields DB1 LEFT JOIN DB2..TableFields DB2 ON DB1.WS_TABLE =
DB2.WS_TABLE
WHERE DB1 = @DBase1
AND DB2 = @Dbase2
AND DB2.WS_FIELD IS NULL
RETURN

Can anybody help me please?

Jul 23 '05 #2
If you want to compare table structures in two databases I suggest you
make use of the Information Schema views. They should provide all the
information you need. You might alternatively consider one of the DB
tools on the market that will help you compare schemas and synchronize
them. See www.red-gate.com for example.

SELECT A.*, B.*
FROM DB1.information_schema.columns AS A
FULL JOIN DB2.information_schema.columns AS B
ON A.table_schema = B.table_schema
AND A.table_name = B.column_name
AND A.column_name = B.column_name
WHERE
COALESCE(A.is_nullable,'')<>COALESCE(B.is_nullable ,'')
OR COALESCE(A.data_type,'')<>COALESCE(B.data_type,'')
... etc

The bad news is that to parameterise the database names you will most
likely want to use Dynamic SQL. Dynamic SQL is really something you
should try to avoid in a production application. I assume that
comparing databases is something you'll only want DB admins to do
rather than users so it's probably a case where Dynamic SQL is a
reasonable solution. Still, if you haven't written dynamic code before
you should read and absorb Erland's superlative article on the subject.
It has all you need to know:

http://www.sommarskog.se/dynamic_sql.html

--
David Portas
SQL Server MVP
--

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dinesh prasad | last post by:
I'm trying to use a servlet to process a form, then send that data to an SQL server stored procedure. I'm using the WebLogic 8 App. server. I am able to retrieve database information, so I know my...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
3
by: Rhino | last post by:
I've spent the last couple of hours trying to figure out how to debug a Java stored procedure and am just going in circles. The last straw came when I got "Cannot open input stream for default"...
4
by: Rhino | last post by:
Is it possible for a Java Stored Procedure in DB2 V7.2 (Windows) to pass a Throwable back to the calling program as an OUT parameter? If yes, what datatype should I use when registering the...
8
by: Thomasb | last post by:
With a background in MS SQL Server programming I'm used to temporary tables. Have just started to work with DB2 ver 7 on z/OS and stumbled into the concept of GLOBAL TEMPORARY TABLE. I have...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
2
by: jed | last post by:
I have created this example in sqlexpress ALTER PROCEDURE . @annualtax FLOAT AS BEGIN SELECT begin1,end1,deductedamount,pecentageextra FROM tax
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.