|
Many thanks for trying! I am afraid that does not work either.
declare @DBName varchar(20)
declare @Str varchar(100)
set @DBName = 'OUTLOG_PACE'
set @Str = 'use ' + @DBName
exec (@Str)
select * from dbo.OUT_tbl_Request
--------------
Error message:
Msg 208, Level 16, State 1, Line 8
Invalid object name 'dbo.OUT_tbl_Request'.
The table name is correct, but this is the result if I start the query in master.
Not to worry, there are only 4 places in the script where the database name needs to be changed. I was just trying to be lazy as it is quite a long script and I did not want to miss one. I will use Ctrl+H !!
|
|
|
|
|
What happens if you put a go statement after the exec? For some reason I remember something similar causing me problems and adding a go statement fixed it. Could be completely wrong as I don't have SQL Server in front of me.
|
|
|
|
|
I think I have found the answer - it don't work!!!
From Microsoft
Using EXECUTE with a Character String
In earlier versions of SQL Server, character strings are limited to 8,000 bytes. This requires concatenating large strings for dynamic execution. In SQL Server 2005, the varchar(max) and nvarchar(max) data types can be specified that allow for character strings to be up to 2 gigabytes of data.
Changes in database context last only until the end of the EXECUTE statement. For example, after the EXEC in this following statement is run, the database context is master.
USE master; EXEC ('USE AdventureWorks; SELECT EmployeeID, Title FROM HumanResources.Employee;');
Thanks very much for trying anyway!
|
|
|
|
|
Hi all,
I would need to sync data between two databases with diferent structure. It happens that in one Database (A) i have just 2 or 3 tables (with lot of fields), and on the other database i should have the same data splitted on many other tables. To Sync this i will to create a lot of triggers that are fired on both database tables but some fields data type were also changed like: Where i have field data type NVarchar() on database A the same correspondent field on Database B is Int(). On database A the field can assume the values Yes/No/In Progress but on databse B the same field assumes the values 1/2/3.
Are a lot of triggers to do!
Wich should be the best way to do this? Wich is the best way to convert the data between tables?
Is there some tool to help me doing this like Sybase ETL?
Best Regards,
|
|
|
|
|
You could possibly use SSIS to achieve most of what you're after. How current do both databases need to be?
|
|
|
|
|
|
Hi Guys
Heres a nice easy one for you DBA's out there to settle my curiosity. (Not sure if this is SQL Specific)
Up until recently, I thought that a database schema described the database objects (tables, fields, data types, relationships etc), where the schema is really a way of orgainising and securing data with a prefix. (eg. to restrict a table of orders to just sales, a schema of sales is created and only sales people will be granted access. The table name will be Sales.Orders)
My question is, what is what I originally thought was a schema called? (the description of the database)
Thanks...
oooo, the Jedi's will feel this one....
|
|
|
|
|
|
You are basically correct in your original thought.
"... the schema defines the tables, the fields in each table, and the relationships between fields and tables." (Wikipedia)
What you got confused is database object security. Something like Sales.Orders would indicate that there is a table,Orders, and it's owner is Sales.
To add to the confusion is the fact that Oracle calls things "schemas" within one database. So you can create a schema for Sales and it will hold all of the objects owned by Sales. Ugh. Have I confused you yet ?
Basically, you were right in the first place. Just don't confuse schemas with how database engines provide security to various objects. (tables,views,procedures,etc)
david
fluent in both MS-SQL and Oracle
|
|
|
|
|
Gotcha... So it's kind of dependent on database brand?
As with SQL, when you create a schema object, and assign a table to that schema to create Sales.Orders, this schema is more of a way of organising and defining ownership, rather than security?
I'm trying to get into databases a bit more. It seems a pretty meaty subject...
oooo, the Jedi's will feel this one....
|
|
|
|
|
Well ... defining ownership implicitly has security implications right?
User "Sales" has full access to table Sales.Order, but user "A" will not have access to this table unless specifically granted.
Are you looking to develop databases for use with a website or a client-server windows application ? The reason for this is that with a website or N-Tier applicaiton typically the database and all of its objects are owned by "dbo" (database owner), you then create 1 user "myappuser" that is granted access to that database and you build your security in the application, not the database. The user, "myappuser" has full access to all aspects of the database and your individual users may need only read access to most tables and update to a select few.
Think about a large company with 10,000 users. Would you want the database administrator to have to create 10,000 user accounts and have 10,000 user connections when the system is running ? Nope. You have 1 user connected to the database and he makes all of the requests on behalf of the users. (This is a very simple description, but you should get the idea.)
CodeProject is a great resource for learning and the community is very helpful.
Keep asking questions and have fun learning.
David
|
|
|
|
|
That is great. Thank you.
I'm mainly looking at building databases for applications. They will be large and complex databases, so I was trying to find ways of simplifiying things. As you said, as users will be using the application to make use of the databases, there would be no reason to complecate things by creating lots of users and schema's (as in the db owner), and to have one that the applicaiton controls.
That in itself saved me some potential headaches. Thanks.
oooo, the Jedi's will feel this one....
|
|
|
|
|
Remember that when you have one user connected to the database and your security is controlled by your application you cannot use triggers to build an auditing system. From the trigger's perspective, there is always one user making changes to the database. If your application needs auditing, remember that you will have to pass the applicaiton logon name through your routines to log it in your audit tables.
Also consider how you would handle security for reporting. You don't want anyone connecting to your database with a third-party tool because it circumvent any security you may have built.
You can solve this by:
1) Don't allow anyone else but your application to connect to the database
2) Create special reporting views or stored procedures that can be used by a third-party tool and still conform to your security requiremnets.
Jeez, lots of things to think about right? Can you tell that I've made a few mistakes in datatbase/application design in my past? I've got the lumps and gray hairs to show for it.
|
|
|
|
|
There is a lot to think about. Way more involved than I first thought. All good though. Creating something so complex (provided it all works ok) must give real job satisfaction. I've only done basic database stuff in the past, queries, basic stored procs. I've not really thought about automated auditing, and I didn't realise you could use triggers. This is good stuff.
Sounds like you have plenty of experiance in this game. I have to say, I can see database design and administration playing quite a major part in my career.
oooo, the Jedi's will feel this one....
|
|
|
|
|
Good Day all
i have a two Tables MTM_ACTV_STAFF_CYCLE and MTM_ACTV_STAFF
MTM_ACTV_STAFF
ID | ACTV | STAFF | CYCLETEMPLATE
==================================
1 | 1 | 3 | 3
2 | 4 | 2 | Null
and
MTM_ACTV_STAFF_CYCLE
ID | IDL | CYCLE
=========================
1 | 1 | 3
2 | 4 | 2
i have the Following SP
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROC [dbo].[sp_Get_Staff_Cycles]
(
@Subject_Descr varchar(30)
)
AS
--Check if the table Exists
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FINAL]'))
drop table [FINAL]
--Get the Cycle template description
Declare @CycletemplateTerm varchar(60)
set @CycletemplateTerm = ( select top 1 t.descr from MTM_ACTV_STAFF mtn
inner join tbl_actv a on a.id = MTN.actv
inner join tbl_cntc cc on a.cntcID = cc.id
inner join tbl_modl m on m.id = cc.modlid
inner join tbl_term t on a.term = t.id
LEFT OUTER JOIN MTM_ACTV_STAFF_CYCLE C
ON C.IDL = MTN.ID
WHERE M.DESCR = @Subject_Descr
)
SELECT [ID] = IDENTITY (int,1,1),A.DESCR AS [ACTIVITY], S.DESCR,
CASE MTN.CYCLETEMPLATE WHEN NULL THEN C.CYCLES
ELSE @CycletemplateTerm
END AS CYCLETEMPLATE
INTO FINAL FROM TBL_STAFF S
INNER JOIN MTM_ACTV_STAFF MTN ON
S.ID = MTN.STAFF
inner join tbl_actv a on a.id = MTN.actv
inner join tbl_cntc cc on a.cntcID = cc.id
inner join tbl_modl m on m.id = cc.modlid
LEFT OUTER JOIN MTM_ACTV_STAFF_CYCLE C
ON C.IDL = MTN.ID
WHERE M.DESCR = @Subject_Descr
now in the above Sp i have the Following Problem. In this Line of code
CASE MTN.CYCLETEMPLATE WHEN NULL THEN C.CYCLES
ELSE @CycletemplateTerm
END AS CYCLETEMPLATE
If the field CYCLETEMPLATE in MTM_ACTV_STAFF table is null i want to use the Values found in table MTM_ACTV_STAFF_CYCLE and Field CYCLES
and another condition again, if there is a value in the MTM_ACTV_STAFF and on the CYCLETEMPLATE then
use the Value in the Variable @CycletemplateTerm
Now my Problem is that instead of bringing 3 its Brings a Sting "Year". there is Prbably something Wrong in my Condiution. Can you please help me in that Regard
Thanks
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.somee.com
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
When you're selecting into @cycletemplateterm you're setting it to t.descr, which I'd assume is where the text "Year" is coming from. Have another look and check which table and column should be populating the variable.
|
|
|
|
|
Good Morning
i have changed it to this and it works
ALTER PROC [dbo].[sp_Get_Staff_Cycles]
(
@Subject_Descr varchar(30)
)
AS
--Check if the table Exists
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FINAL]'))
drop table [FINAL]
--Get the Cycle template description
Declare @CycletemplateTerm varchar(60)
set @CycletemplateTerm = ( select top 1 t.descr from MTM_ACTV_STAFF mtn
inner join tbl_actv a on a.id = MTN.actv
inner join tbl_cntc cc on a.cntcID = cc.id
inner join tbl_modl m on m.id = cc.modlid
inner join tbl_term t on a.term = t.id
LEFT OUTER JOIN MTM_ACTV_STAFF_CYCLE C
ON C.IDL = MTN.ID
WHERE M.DESCR = @Subject_Descr
)
SELECT [ID] = IDENTITY (int,1,1),A.DESCR AS [ACTIVITY], S.DESCR,
CASE
WHEN MTN.CYCLETEMPLATE IS NULL THEN C.CYCLES
WHEN MTN.CYCLETEMPLATE IS NOT NULL THEN @CycletemplateTerm
END AS CYCLETEMPLATE
INTO FINAL FROM TBL_STAFF S
INNER JOIN MTM_ACTV_STAFF MTN ON
S.ID = MTN.STAFF
inner join tbl_actv a on a.id = MTN.actv
inner join tbl_cntc cc on a.cntcID = cc.id
inner join tbl_modl m on m.id = cc.modlid
LEFT OUTER JOIN MTM_ACTV_STAFF_CYCLE C
ON C.IDL = MTN.ID
WHERE M.DESCR = @Subject_Descr
Thank you
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.somee.com
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswa@its.co.za
http://www.itsabacus.co.za/itsabacus/
|
|
|
|
|
Hi,
How to select a row which contain null where i have a condition also.
eg:
field NUMBER have -1,null,1,2,3,4 values
how can i select the value excluding -1
i give a condition
select * from table where NUMBER <> -1,
its returning only 1,2,3,4
I want null row also
Regards,
YPKI
|
|
|
|
|
SELECT *
FROM [table]
WHERE [number] <> -1
OR [number] IS NULL
I are troll
|
|
|
|
|
SELECT *
FROM dbo.tblDisplayPositive
WHERE FieldNumber >= 0
OR FieldNumber IS NULL
Hope this helps
Niladri Biswas
|
|
|
|
|
Hi,
I need to submit data from a form to an SQL2005 database via a stored procedure. The difficult part is that i also need to get 5 random records from a secondary table and insert these as part of the inserted record in table 1.
My structure is akin to this:
Tbl_Campaigns (table to be inserted to)
Key | field_1 | field_2 | field_3 | field_4 | field_5 etc
Tbl_Organisations (table to get the 5 random records from)
Key | field_1
I need to get 5 unique / random records from 'tbl_Organisations', catch them and insert them into a single record along with the data input to the stored procedure. I understand arrays arent an option in SQL2005(?).
So how do i catch these records and then insert them as a single record along with the SP Inputs?
Any help would be greatly appreciated as i am trying my best to get to grips with the complexities of SQL
Thanks.
modified on Tuesday, July 7, 2009 6:04 AM
|
|
|
|
|
This may be useful....
select top 5 key, field_1 from tbl_organisations order by 100 * rand()
However, I haven't tested it cos I don't have SQL Server here so it may not work at all!
|
|
|
|
|
Hi,
I would get a random record using the following:
SELECT TOP 5 (*) from tbl_organisations AS randomOrgs
However my issue is with how i would then catch these 5 records and insert them into the other table as a single record with the SP inputs.
|
|
|
|
|
SELECT TOP 5 (*) from tbl_organisations order by newid()
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
Blue_boy,
Please read the original post in this thread. Thanks for your help but you appear to have misinterpretated my question.
|
|
|
|