Showing posts with label t-sql. Show all posts
Showing posts with label t-sql. Show all posts

Wednesday, March 21, 2012

How to find the SQL Server install directory from tsql

How do we find the SQL Server install directory using a T-sql command or from command prompt?

Thanks,
Vivek
You can find the path (with and without filename) to master.mdf from the master_files system view, if that's enough:

Code Snippet

select physical_name
from master.sys.master_files
where name = 'master';

declare @.tail int
set @.tail = (
select charindex('\',reverse(physical_name))
from master.sys.master_files
where name = 'master'
)
select substring(physical_name,1,len(physical_name)-@.tail)
from master.sys.master_files
where name = 'master';

The installation directory is a registry key, and a batch file to dig it out is suggested here: http://weblogs.asp.net/jgalloway/archive/2006/10.aspx. (I didn't try it, but there's a link to another page noting that the white space after delims is a tab followed by a space.)

Steve Kass
Drew University
http://www.stevekass.com
|||

All you need is in the link below but in C#. It is 23pages long so read it but you need admin permissions if it is a server. Hope this helps.

http://msdn2.microsoft.com/en-us/library/bb264562.aspx

|||The system databases are not under the SQL Server installed directory. They are in a different drive. If this is the case how do i get the install directory?
|||

One more approach on T-SQL itself..

Code Snippet

Create Table #Data

(

path Varchar(max)

)

Insert Into #Data

Exec xp_cmdshell 'path'

Declare @.Paths Varchar(max)

Set @.Paths = ';'

Select @.Paths = @.Paths + Replace(Path,'Path=','') From #Data Where Path is NOT NULL

select @.Paths = @.Paths + ';'

Create table #Number

(

Number Int

)

Declare @.i as Int

Set @.i = 1

While @.i<len(@.Paths)

Begin

Insert Into #Number Select @.i;

Set @.i=@.i+1

End

Select

Replace(Replace(paths,'\Tools\BINN\',''),'\Tools\BINN','') [SQL Server Paths]

From

(

Select

Substring(@.Paths,Number,Charindex(';',@.Paths,Number)-Number) Paths

From

#Number

Where

Substring(@.paths,Number-1,1) = ';'

) as Data

Where Paths Like '%Microsoft SQL Server%'

Drop Table #Number;

Drop Table #Data;

|||

One more easy approach .. using undocumented system procedure..

Code Snippet

Declare @.Path as varchar(100);

Set @.Path = NULL

Exec master..xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Microsoft SQL Server\70\Tools\ClientSetup', 'SQLPath', @.Path OUTPUT

Select @.Path as [Sql Server 7.0 path]

Set @.Path = NULL

Exec master..xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Microsoft SQL Server\80\Tools\ClientSetup', 'SQLPath', @.Path OUTPUT

Select @.Path as [Sql Server 2000 path]

Set @.Path = NULL

Exec master..xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Microsoft SQL Server\90\Tools\ClientSetup', 'SQLPath', @.Path OUTPUT

Select @.Path as [Sql Server 2005 path]

Set @.Path = NULL

Exec master..xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\ClientSetup', 'SQLPath', @.Path OUTPUT

Select @.Path as [Sql Server KATMAI path]

|||

You could use SMO and write a CLR function that would return the value.

It could be more reliable (thinking permission issues) than reading the registry with xp_regread.

For an idea, refer to Books Online, Topic: 'RootDirectory property'

There are several properties of the Server object that one could find useful.

|||Thanks Manivannan it works!!!
|||

The link below contains a SQLCLR TVF that can be used to get this information and others which are persisted in the registry only. For SQL Server 2000, you will have to access the registry keys directly from a batch file or use system databases path (which may be configured during setup to different location).

http://blogs.msdn.com/sqltips/archive/2005/08/19/SqlRegSettings.aspx

How to find the Froiengn key refrence column and refrence table through T-SQL

Hi,
I am creating a tool for generating SQL Scripts.
I want the sql statement for getting the table name , column name ,
reference table name ,reference column name of a particular foreign key.
In this case I know only the foreign key name.
Thanks and Regards,
SathiamoorthyOJ has written this script
create procedure usp_findreferences
@.tbname sysname=null
as
set nocount on
Print 'Referenced:'
select c1.table_name,
c1.column_name,
fkey=r.constraint_name,
referenced_parent_table=c2.table_name,
c2.column_name
from information_schema.constraint_column_usage c1 join
information_schema.referential_constraints r on
c1.constraint_name=r.constraint_name
join information_schema.constraint_column_usage c2 on
r.unique_constraint_name=c2.constraint_name
where c1.table_name=coalesce(@.tbname,c1.table_name)
order by case when @.tbname is null then c1.table_name else c2.table_name end
print ''
print 'Referencing:'
select c1.table_name,
c1.column_name,
fkey=r.constraint_name,
referencing_child_table=c2.table_name,
c2.column_name
from information_schema.constraint_column_usage c1 join
information_schema.referential_constraints r on
c1.constraint_name=r.unique_constraint_name
join information_schema.constraint_column_usage c2 on
r.constraint_name=c2.constraint_name
where c1.table_name=coalesce(@.tbname,c1.table_name)
order by case when @.tbname is null then c1.table_name else c2.table_name end
go
--test run
exec usp_findreferences 'Orders'
drop proc usp_findreferences
"Sathiamoorthy" <someone@.microsoft.com> wrote in message
news:OZP0tPJLGHA.536@.TK2MSFTNGP09.phx.gbl...
> Hi,
> I am creating a tool for generating SQL Scripts.
> I want the sql statement for getting the table name , column name ,
> reference table name ,reference column name of a particular foreign key.
> In this case I know only the foreign key name.
> Thanks and Regards,
> Sathiamoorthy
>|||Thank you very much.
sathyamoorthy
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:uGo7LUJLGHA.1180@.TK2MSFTNGP09.phx.gbl...
> OJ has written this script
> create procedure usp_findreferences
> @.tbname sysname=null
> as
> set nocount on
>
> Print 'Referenced:'
> select c1.table_name,
> c1.column_name,
> fkey=r.constraint_name,
> referenced_parent_table=c2.table_name,
> c2.column_name
> from information_schema.constraint_column_usage c1 join
> information_schema.referential_constraints r on
> c1.constraint_name=r.constraint_name
> join information_schema.constraint_column_usage c2 on
> r.unique_constraint_name=c2.constraint_name
> where c1.table_name=coalesce(@.tbname,c1.table_name)
> order by case when @.tbname is null then c1.table_name else c2.table_name
end
>
> print ''
> print 'Referencing:'
> select c1.table_name,
> c1.column_name,
> fkey=r.constraint_name,
> referencing_child_table=c2.table_name,
> c2.column_name
> from information_schema.constraint_column_usage c1 join
> information_schema.referential_constraints r on
> c1.constraint_name=r.unique_constraint_name
> join information_schema.constraint_column_usage c2 on
> r.constraint_name=c2.constraint_name
> where c1.table_name=coalesce(@.tbname,c1.table_name)
> order by case when @.tbname is null then c1.table_name else c2.table_name
end
> go
>
> --test run
> exec usp_findreferences 'Orders'
> drop proc usp_findreferences
> "Sathiamoorthy" <someone@.microsoft.com> wrote in message
> news:OZP0tPJLGHA.536@.TK2MSFTNGP09.phx.gbl...
>

Friday, March 9, 2012

How to find out CPU saturation in T-SQL

Hello,
I was wondering if there is a way to find out in T-SQL how busy CPU is. I
would like to get the same information as shown in CPU usage graph in Task
Manager.
Thanks,
IgorThere are other processes other than SQL Server that consume CPU resources.
Perhaps you can get this information using WMI.
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:elq%23aIoKFHA.3960@.TK2MSFTNGP09.phx.gbl...
> Hello,
> I was wondering if there is a way to find out in T-SQL how busy CPU is. I
> would like to get the same information as shown in CPU usage graph in Task
> Manager.
> Thanks,
> Igor
>|||I was hoping to obtain this information using T-SQL.
<WKidd> wrote in message news:%23ImbyToKFHA.1284@.TK2MSFTNGP14.phx.gbl...
> There are other processes other than SQL Server that consume CPU
> resources. Perhaps you can get this information using WMI.
> "Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
> news:elq%23aIoKFHA.3960@.TK2MSFTNGP09.phx.gbl...
>|||Igor Marchenko wrote:
> Hello,
> I was wondering if there is a way to find out in T-SQL how busy CPU
> is. I would like to get the same information as shown in CPU usage
> graph in Task Manager.
> Thanks,
> Igor
Not for the entire server. Only for SQL Server.
David Gugick
Imceda Software
www.imceda.com|||Could you please tell me how to do it for SQL server? I would like to know
how much of CPU percent wise is used by SQL server. Similar to what is shown
in process tab of Task bar.
Thanks,
Igor
"David Gugick" <davidg-nospam@.imceda.com> wrote in message
news:OJfqq4pKFHA.3552@.TK2MSFTNGP12.phx.gbl...
> Igor Marchenko wrote:
> Not for the entire server. Only for SQL Server.
> --
> David Gugick
> Imceda Software
> www.imceda.com|||You can use perfmon to see how much cpu SQL server is using, and it auto
refreshes so you watch and record performance.
Under "Process" performance object, select "% Processor Time", and in the
"Select instance from a list" section, select "sqlservr".
Obviously this is not a t-sql solution, but probably more intuitive,
especially if you want to log the results to review later.
Simon Worth
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:uvuFOvxKFHA.440@.TK2MSFTNGP10.phx.gbl...
> Could you please tell me how to do it for SQL server? I would like to know
> how much of CPU percent wise is used by SQL server. Similar to what is
shown
> in process tab of Task bar.
> Thanks,
> Igor
> "David Gugick" <davidg-nospam@.imceda.com> wrote in message
> news:OJfqq4pKFHA.3552@.TK2MSFTNGP12.phx.gbl...
>|||Igor Marchenko wrote:
> Could you please tell me how to do it for SQL server? I would like to
> know how much of CPU percent wise is used by SQL server. Similar to
> what is shown in process tab of Task bar.
> Thanks,
> Igor
From T-SQL, you can use the @.@.CPU_BUSY global variable along with
@.@.TIMETICKS. You'll have to search Google for the required calculation
as you'll have to compare the toal cpu used over an interval as compared
to the total time.
David Gugick
Imceda Software
www.imceda.com

How to find out CPU saturation in SQL server

Hello,
I was wondering if there is a way to find out in T-SQL how busy CPU is. I
would like to get the same information as shown in CPU usage graph in Task
Manager.
Thanks,
Igor
Performance Monitor ?
Gopi
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:%23d21PIoKFHA.2936@.TK2MSFTNGP15.phx.gbl...
> Hello,
> I was wondering if there is a way to find out in T-SQL how busy CPU is. I
> would like to get the same information as shown in CPU usage graph in Task
> Manager.
> Thanks,
> Igor
>
|||I would like to ontain this information programmatically ideally using
T-SQL.
Thanks,
Igor
"rgn" <gopinathr@.healthasyst.com> wrote in message
news:%23y8EvkvKFHA.3960@.TK2MSFTNGP09.phx.gbl...
> Performance Monitor ?
> Gopi
> "Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
> news:%23d21PIoKFHA.2936@.TK2MSFTNGP15.phx.gbl...
>

How to find out CPU saturation in SQL server

Hello,
I was wondering if there is a way to find out in T-SQL how busy CPU is. I
would like to get the same information as shown in CPU usage graph in Task
Manager.
Thanks,
IgorPerformance Monitor ?
Gopi
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:%23d21PIoKFHA.2936@.TK2MSFTNGP15.phx.gbl...
> Hello,
> I was wondering if there is a way to find out in T-SQL how busy CPU is. I
> would like to get the same information as shown in CPU usage graph in Task
> Manager.
> Thanks,
> Igor
>|||I would like to ontain this information programmatically ideally using
T-SQL.
Thanks,
Igor
"rgn" <gopinathr@.healthasyst.com> wrote in message
news:%23y8EvkvKFHA.3960@.TK2MSFTNGP09.phx.gbl...
> Performance Monitor ?
> Gopi
> "Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
> news:%23d21PIoKFHA.2936@.TK2MSFTNGP15.phx.gbl...
>

How to find out CPU saturation in SQL server

Hello,
I was wondering if there is a way to find out in T-SQL how busy CPU is. I
would like to get the same information as shown in CPU usage graph in Task
Manager.
Thanks,
IgorPerformance Monitor ?
Gopi
"Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
news:%23d21PIoKFHA.2936@.TK2MSFTNGP15.phx.gbl...
> Hello,
> I was wondering if there is a way to find out in T-SQL how busy CPU is. I
> would like to get the same information as shown in CPU usage graph in Task
> Manager.
> Thanks,
> Igor
>|||I would like to ontain this information programmatically ideally using
T-SQL.
Thanks,
Igor
"rgn" <gopinathr@.healthasyst.com> wrote in message
news:%23y8EvkvKFHA.3960@.TK2MSFTNGP09.phx.gbl...
> Performance Monitor ?
> Gopi
> "Igor Marchenko" <igormarchenko@.hotmail.com> wrote in message
> news:%23d21PIoKFHA.2936@.TK2MSFTNGP15.phx.gbl...
>> Hello,
>> I was wondering if there is a way to find out in T-SQL how busy CPU is. I
>> would like to get the same information as shown in CPU usage graph in
>> Task Manager.
>> Thanks,
>> Igor
>

Sunday, February 19, 2012

How to filter a table to another by T-SQL

I have 2 tables: table one is for all the data, table two is filtered data
from table one. I want to shedule this job and do it every night, i.e. every
end of the day (00:00), I want to use a sheduled T-Sql statement to get all
today's data from table one and insert those meet the criteria into table
two.
I don't know how to:
1. Where to schedule such kind of a job?
2. What T-SQL should I use? SELECT INTO seems like not be able to add
records to a existing table."JL" <ljmagzine@.hotmail.com> wrote in message
news:O0kCcf00DHA.3216@.TK2MSFTNGP11.phx.gbl...
> I have 2 tables: table one is for all the data, table two is filtered data
> from table one. I want to shedule this job and do it every night, i.e.
every
> end of the day (00:00), I want to use a sheduled T-Sql statement to get
all
> today's data from table one and insert those meet the criteria into table
> two.
> I don't know how to:
> 1. Where to schedule such kind of a job?
> 2. What T-SQL should I use? SELECT INTO seems like not be able to add
> records to a existing table.
>
Sql Server Agent can schedule the job.
Make it a TSQL job, and run something like
"
delete from table2
insert into table2
select * from table1
where col=1234
"
David|||Hi,
1. Where to schedule such kind of a job?
Use SQL Agent to Schedule the Job. But Ensure that SQL Agent service
runs all the time. Go to
How to:
1. Select Enterprise manager
2. Choose Management
3. Select SQL Server Agent option
4. Select Jobs
5. Right click above jobs and create new Job
6. There in step option you can create a TSQL Job and use schedule
option to schedule the Job
2. What T-SQL should I use? SELECT INTO seems like not be able to add
records to a existing table.
Use Insert into Select statement , the statement looks like
Insert into table2(columns) select column1,col2 from table1 where
conditions.....
The above statement you can incorporate inside your job ...Step.
Thanks
Hari
MCDBA
"JL" <ljmagzine@.hotmail.com> wrote in message
news:O0kCcf00DHA.3216@.TK2MSFTNGP11.phx.gbl...
> I have 2 tables: table one is for all the data, table two is filtered data
> from table one. I want to shedule this job and do it every night, i.e.
every
> end of the day (00:00), I want to use a sheduled T-Sql statement to get
all
> today's data from table one and insert those meet the criteria into table
> two.
> I don't know how to:
> 1. Where to schedule such kind of a job?
> 2. What T-SQL should I use? SELECT INTO seems like not be able to add
> records to a existing table.
>