|
SQL
Server 2000 FAQs Details of "Frequently Asked Questions" (FAQ) dealing with common SQL Server 2000 problems. SQL Server 7.0 Frequently Asked Questions Q. I want to use a Primary key constraint to uniquely identify all employees, but I also want to check that the values entered are in a particular range, and are all non-negative. How can I set up my constraint to do this? A. It’s a simple matter of setting up two constraints on the employee id column: the primary key constraint to guarantee unique values, and a check constraint to set up the rule for a valid, non-negative range; for example:
Create Table Employee ( Emp_Id Integer Not Null Constraint PkClustered
Primary Key Or, if you want to create table-level constraints (where more than one column defines the primary key):
Create Table Employee ( Emp_Id Int Not Null , Emp_Name VarChar(30) Not Null,
A. There are probably a number of ways, but this may be a job for a cursor. Try the following Transact SQL script:
Declare @Name VarChar (50)
Open Cursor_ScriptTriggerDrop
A. Take a deep breath and build it yourself. Here’s a useful function to give you sophisticated formatting abilities: Use
Pubs
-- Insert Day:
-- Insert Month:
-- Insert the Year:
-- Return function value: -- Examples: Set NoCount On
Select dbo.FormatDate(Ord_Date,'dddd, mmmm d1, yyyy') From Pubs..Sales
Set NoCount Off
A. I’m sure this question has come up before, relating to SQL Server 7, but it remains a good question. The answer’s ‘yes and no’ (though more yes than no). It’s true that both SQL Server 7 and 2000 do ad hoc caching/ parameterization of SQL statements, but there are a couple of ‘gotchas’ attached. First of all the following two statements will generate an executable plan in SQL Server’s cache with a usage count of two (i.e. the plan is put in cache and reused by the second statement):
Select Title From Pubs.Dbo.Titles Where Price = £20 However if the currency indicator is dropped, one plan is marked with an Integer parameter, the other with a Numeric parameter (as each is auto-parameterized). Worse still, if the case of any part of the column name(s), database, owner, or object name(s) changes, in a following SQL statement, SQL Server fails to recognize the structural identity of the next statement and generates another plan in cache. To check this out, run both statements (with currency marker and identical case) along with the following call: Select CacheObjType , UseCounts , Sql From Master..sysCacheObjects (If possible run Dbcc FreeProcCache before the three SQL statements, but beware: it drops all objects in cache). You should find that the executable plan has been found and reused (the UseCounts value will be ‘2’). Now, either drop the currency indicators or change the case in one of the ‘Select Title … ‘ statements and note that a fresh executable (and compiled) plan is generated for the second statement. Now create a stored procedure with Price as an input parameter, run it with or without currency markers, changing the case of the call at will, and the procedure will be found and reused – check the UseCounts column value.
A. Here’s a stored procedure which will give you a list of all options set in a session. If you create it in Master, with the ‘sp_’ prefix it’ll be available within any database:
Create Procedure sp_GetDBOptions
If
@@Error <> 0
/*
Insert values into the Temporary table */
If
@@Options <> 0 Set NoCount Off
A. Perfectly easy, I’ll even order the output by type and name:
Select type, name From Master..sysObjects Where Type In ('S', 'V')
A. Yes, there’s an undocumented procedure called sp_MsHasDbAccess which gives precisely the information you want. Use the SetUser command to impersonate a user (SetUser loginname) and run sp_MsHasDbAccess with no parameters. To revert to your default status, run SetUser with no parameters.
Other recommended sources for Microsoft SQL FAQs: http://support.microsoft.com/support/default.asp?PR=sql&FR=0&SD=MSDN&LN=EN-US (for FAQ and Highlights for SQL) http://support.microsoft.com/view/dev.asp?id=hl&pg=sql.asp for Microsoft technical advice) http://support.microsoft.com/support/sql/70faq.asp (UK based) http://www.mssqlserver.com/faq/ (an independent support company) http://www.sqlteam.com/ (a good source for answers to SQL questions) http://www.sql-server-performance.com/default.asp (for SQL performance issues) http://www.sql-server.co.uk/frmMain.asp (UK SQL user group - need to do free registration) http://www.advisor.com/wHome.nsf/w/MMB (a useful US based VB/SQL Magazine) http://www.advisor.com/wHome.nsf/w/JMSS (a useful US based SQL Magazine) http://secure.duke.com/nt/sqlmag/newsub.cfm?LSV=NunLjpaxugA-hA9hauyikjkarnlkTKgEAQ&Program=9 (another useful SQL magazine) The public newsgroups starting microsoft.public.sqlserver are also useful sources for help with individual problems. If you still have problems please complete the Technical Support Form and email or fax it to us. |
||||||||||||
© F1 Computing Systems Ltd 2002 |
|||||||||||||