|
ASAIK, you can only create a table[^] variable, fill it with data and then use in SELECT statements.
imnotso# wrote: Just want to use a variable to access an exisiting table.
BTW, why do you want to do something like this?
|
|
|
|
|
have the sql query in a string variable and execute using sp_executesql or EXECUTE Command. something like this should work, try...
@SqlQuery = 'Select * from ' + @MyParam<br />
EXEC sp_executesql @SqlQuery
Regards - J O H N -
|
|
|
|
|
You should always enclosed the table name in side square brackets just in case it contains any odd characters and the table name should be checked to see if the table exists first. These are very basic security precautions against invalid or maliceous data from causing problems with the database.
|
|
|
|
|
Thanks John,
but
declare @SqlQuery varchar
set @SqlQuery = 'Select * from raiser wHERE raiser.raiserid=3'
EXEC @SqlQuery
Server: Msg 2812, Level 16, State 62, Line 5
Could not find stored procedure 'S'.
doesn't work
what am I doing wrong?
|
|
|
|
|
|
hi there, in this example you didn't create any table. You creating a variable and it is not possible to select from variable,
--
Declare @MyParam varchar (20)
Set @MyParam = 'MyTableName'
you can simple select like this:
SELECT @MyParam
-- it will give this result 'MyTableName'
if you want to create table, use this
Create Table #MyTableName
(
here you define the attributes
ex.
atributename varchar(20)
)
and then you can use
SELECT * FROM #MyTableName
the result --- atributename
respect
spaps
|
|
|
|
|
Cheers guys, job done via Dynamic SQL.
Why I can't lookup a variable table name, who knows? Seems stupid to not be able to.
Thanks again.
|
|
|
|
|
imnotso# wrote: Why I can't lookup a variable table name, who knows?
Because it's not part of the language specification.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Hi,
My aim is to bind the data dynamically to a label ..the database used is MS Access
I have created a module for creation of a database and inserting values in it through a gui n a canvas on which labels are dragged and dropped i want to print the form dynamically the process is getting values from the database n printing i know how to print but not how to bind the data i have tried with label1.databinding but failed as labels are also generated dynamically i cant bind them using properties window
if any one knows how to bind dynamically data please let me know..
regards
sindhu tiwari
its me sid
|
|
|
|
|
Hi,
How can I add a new column in middle/(at any position) of the existing table?
How can I do that in Postgres?
Any help would be appreciated.
Selva
|
|
|
|
|
hi
plz make use of Sql Help
hint: you can achieve this using ALTER TABLE Command
ALTER TABLE TABLENAME ADD COLUMNNAME DATATYPE(SIZE)
-- modified at 6:11 Tuesday 13th November, 2007
|
|
|
|
|
Thanks for the reply.
But it will add the new column at the last.
But i want it to get added at the middle.
Is there any way to achieve this.
Selva
|
|
|
|
|
Column ordering is not important within a SQL server database - why do you need to do this.
|
|
|
|
|
SelvaKr wrote: But i want it to get added at the middle.
Is there any way to achieve this.
If it was me I would start by questioning the requirement, because it suggests that someone either doesn't know how databases work and/or are using it incorrectly for some other purpose.
But excluding that you would have to
1. Create temporary table.
2. Copy all of the data to the temp table.
3. Delete the original table.
4. Re-create with the new column
5. Copy all of the data back to the newly created table.
6. Delete the temp table.
Obviously you will need to deal with foreign keys appropriately. And it is likely that that the database can not be active (not processing work) while this is going on.
|
|
|
|
|
Hi
was wondering if anybody could help me. I was maid stored procedure as following:
USE [Spacva]
GO
/****** Object: StoredProcedure [dbo].[Test] Script Date: 11/13/2007 11:12:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Test]
(
@START_DATE smalldatetime,
@END_DATE smalldatetime
)
AS
BEGIN
SELECT VOZILA.ID_VOZILA, VOZILA.PROIZVODNI_POGON, VOZILA.NAZIV, VOZILA.TIP, VOZILA.REG,
SUM(TOCENJE.KOL_L) AS Quantity, SUM(TOCENJE.SATI_RADA) AS Hours, SUM(TOCENJE.PROSJEK_L_PO_H) AS Average
FROM VOZILA INNER JOIN
TOCENJE ON VOZILA.ID_VOZILA = TOCENJE.ID_VOZILA
WHERE (TOCENJE.DATUM_UNOSA BETWEEN CONVERT(SMALLDATETIME, @START_DATE, 104) AND CONVERT(SMALLDATETIME, @END_DATE, 104))
GROUP BY VOZILA.ID_VOZILA, VOZILA.PROIZVODNI_POGON, VOZILA.NAZIV, VOZILA.TIP, VOZILA.REG
END
That stored procedure I was include in my Windows C# Project, just drag and drop to Win Form. My question is, how to add footer in established DataGrid, I mean how to add SUM of all selected values from column Quantity to my DataGrid?
I'm new in SQL, Visual .NET, C# so thanks in advance for help!
Zvonimir
|
|
|
|
|
You either have two options. Either manually iterate through the items in the datagrid after it is populated, do the sum and add a row. Or you can do some 'ROLLUP' functionality in the SQL query so when you pull it down into the datagrid, it already has the sum values you want in their own row(s).
|
|
|
|
|
Can some one tell me what's the difference between nvarchar and varchar. Similary for nchar and char
Regards,
LEE
|
|
|
|
|
|
http://geekswithblogs.net/claeyskurt/archive/2006/02/04/68161.aspx
|
|
|
|
|
hi
line by line debugging of a Stored Procedure is possible in Sql Server 2000.?
Regards,
Joe
|
|
|
|
|
|
thnx for the reply
i will check it out...
Regards
Joe
|
|
|
|
|
I have a problem with one of my sql query
Start Date is 10/11/2007 (dd/mm/yyy format)
End Date :10/12/2007
I have to get all dates listed from 11/11/2007 to 9/12/2007 (all dates between Start Date & End Date
Pls help me...
Sunil Nair
|
|
|
|
|
select * from table1 where mydate between @startdate and @enddate . Take a look at UTC and datetime formats to make sure that you are working with your date properly (you are using a stored procedure aren't you - and you are passing @startdate and @enddate in as datetime).
-- modified at 4:36 Tuesday 13th November, 2007
Ah - I see from others that you are trying to solve a different problem. Oh well.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Pete,
Unless and untill we get to know what solution was exactly required, we are not very sure of the problem.
Regards,
Adeel
Do rate the reply, if it helps or even if it doesnot, because it helps the members to know, what solved the issue. Thanks.
|
|
|
|