473,382 Members | 1,425 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,382 software developers and data experts.

How to use value of a variable in defining data type

HI Experts,

I have same table structures in two database and one master table which contains Table id, Table name,primary key, data type of primary key. i have to comapare
Tables in both tha database and as per result i have to do insert,update or delete.

for that i have written query :


DECLARE @rowcount_mastertable FLOAT
SET @rowcount_mastertable = (select count(*) from master_table)

DECLARE @TABLE_ID float,
@TABLE_NAME varchar (100),
@primary_key varchar (100),
@Primarykey_DATATYPE varchar (50),

DECLARE @COUNTER FLOAT
SET @COUNTER = 1

WHILE (@Counter <= @rowcount_mastertable)

Begin

SET @TABLE_NAME = (SELECT TABLE_NAME FROM MASTER_TABLE TABLE_ID = @COUNTER)
SET @primary_key = (SELECT primary_key FROM MASTER_TABLE WHERE TABLE_ID = @COUNTER)
SET @Primarykey_DATATYPE = (SELECT Primarykey_DATATYPE FROM MASTER_TABL WHERE TABLE_ID = @COUNTER)

--In below line i want to declare a variable and datatype should be same as what we got from master table so that i can use this @MAX_primary_key to fetch max of primary key from table name where table id is 1

DECLARE @MAX_primary_key @Primarykey_DATATYPE
SELECT @MAX_primary_key = MAX(@primary_key) FROM @TABLE_NAME
WHERE TABLE_ID = @COUNTER


---But by running it i am getting error that "Incorrect syntax near '@Primarykey_DATATYPE'. and "Must declare the variable '@MAX_primary_key'.


Please suggest

Thanks in Advance
Dec 14 '07 #1
1 1394
Jim Doherty
897 Expert 512MB
HI Experts,

I have same table structures in two database and one master table which contains Table id, Table name,primary key, data type of primary key. i have to comapare
Tables in both tha database and as per result i have to do insert,update or delete.

for that i have written query :


DECLARE @rowcount_mastertable FLOAT
SET @rowcount_mastertable = (select count(*) from master_table)

DECLARE @TABLE_ID float,
@TABLE_NAME varchar (100),
@primary_key varchar (100),
@Primarykey_DATATYPE varchar (50),

DECLARE @COUNTER FLOAT
SET @COUNTER = 1

WHILE (@Counter <= @rowcount_mastertable)

Begin

SET @TABLE_NAME = (SELECT TABLE_NAME FROM MASTER_TABLE TABLE_ID = @COUNTER)
SET @primary_key = (SELECT primary_key FROM MASTER_TABLE WHERE TABLE_ID = @COUNTER)
SET @Primarykey_DATATYPE = (SELECT Primarykey_DATATYPE FROM MASTER_TABL WHERE TABLE_ID = @COUNTER)

--In below line i want to declare a variable and datatype should be same as what we got from master table so that i can use this @MAX_primary_key to fetch max of primary key from table name where table id is 1

DECLARE @MAX_primary_key @Primarykey_DATATYPE
SELECT @MAX_primary_key = MAX(@primary_key) FROM @TABLE_NAME
WHERE TABLE_ID = @COUNTER


---But by running it i am getting error that "Incorrect syntax near '@Primarykey_DATATYPE'. and "Must declare the variable '@MAX_primary_key'.


Please suggest

Thanks in Advance
Examine this line more closely...

DECLARE @MAX_primary_key @Primarykey_DATATYPE

at what point are you declaring either of these variable datatypes?

Jim :)
Dec 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
6
by: Steve Jorgensen | last post by:
Many of the regulars here have explained that declaring variables using As New .... is a bad idea, and some have given some good explanations, but I wanted add one more demonstration to the mix. ...
24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
20
by: MLH | last post by:
120 MyString = "How many copies of each letter do you need?" 150 MyVariant = InputBox(MyString, "How Many?", "3") If MyVariant = "2" Then MsgBox "MyVariant equals the string '2'" If...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
42
by: polas | last post by:
Afternoon all, I have some code (which I thought was OK, but now appreciate is probably not by the standard) which contains int a; ...... if (a==NULL) { ....
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.