This statement will display the datetime stamp of the last user scan and the last user update. It will also include the # of user updates on that table.
SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
WHERE database_id = DB_ID( 'yourDatabaseName')
This statement will work
if object_id('tempdb..#mytempTbl') is not null
drop table #mytempTbl
For an effective, simple HTML formatted static list go into an MSDOS command window and type:
wmic qfe list full /format:htable > c:\winpatches.htm
Open up a windows command shell. Type in:
shutdown -r -t360 -m servname.onyourdomain.com
If you are looking for more information about this, check out this link
This took me longer than it should have to figure out. In just a few simple steps, all those emails can easily be dealt with in sets.
- Go to the folder you are interested in.
- click the top check box--or the "select all" . You'll see a text line appearing just above the trash can icon that says something like "All 999 conversations on this page are selected." (see below).
- Click on the underlined text that says "Select all 999 conversations in YourFileFolderName". The text line will change to something like "All 999 conversations in 'YourFileFolderName' are selected."
- Click the Delete button. It will pop up with a 'bulk action' message about the delete you're about to do. Agree to delete.
If you have a very large data base (VLDB) a select count(*) can take a long time to resolve. Try this statement as an alternative:
,SUM(p.row_count) AS 'RowCount' FROM sys.dm_db_partition_stats p JOIN sys.objects o ON o.object_id = p.object_id JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE p.index_id < 2 AND o.type = 'U'
This will locate specific text within an object on a given database. In this case, it will find all views, stored procs which have the text string "Department" somewhere in the body.
o.name AS Object_Name,
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id = o.object_id
WHERE m.definition Like '%Department%';
The most likely reason is that your profile has not been configured, or you are using an incorrect name under the @profile_name parameter. To determine your profile settings, use this query:
If after you have checked the results of this query and you are using the correct profile name, check how you are passing the parameter values.
Don't do this:
exec msdb.dbo.sp_send_dbmail @subject, @body, @profile_name, @recipients
... it will still fail because you need to explicitly define the parameter values ...
@subject = 'Some topic or another'
So you would either have to do this:
exec msdb.dbo.sp_send_dbmail @subject = 'Some topic or another', @body = 'read this!' ...etc.,
declare @subj varchar(100)
set @subj 'Some topic or another'
exec msdb.dbo.sp_send_dbmail @subject=@subj ... etc.,...
This sql script will start an agent job on a remote server. If you're running this as a step in another agent job, keep in mind that the job you are running it from will be determined to be successful, even if the remote job fails--as this is an asynchronous kick off only.
declare @returnCode int
declare @JobName varchar(300)
declare @ServerName varchar(200)
declare @query varchar(8000)
declare @cmd varchar(8000)
set @JobName = 'TheJobNameYouWantToRun'
set @ServerName = 'TheRemoteServerWhereTheJobIs'
set @query = 'exec msdb.dbo.sp_start_job @JobName = ''' + @JobName + ''''
set @cmd = 'osql -E -S ' + @ServerName + ' -Q "' + @query + '"'
print ' @JobName = ' +isnull(@JobName,'NULL @JobName')
print ' @ServerName = ' +isnull(@ServerName,'NULL @ServerName')
print ' @query = ' +isnull(@query,'NULL @query')
print ' @cmd = ' +isnull(@cmd,'NULL @cmd')
exec @returnCode = master.dbo.xp_cmdshell @cmd
if @returnCode <> 0 or @returnCode is null
print 'xp_cmdshell @returnCode = '+isnull(convert(varchar(20),@returnCode),'NULL @returnCode')
You will need to enable the feature first.
Follow these steps:
EXEC sp_configure 'show advanced options', 1
-- this updates whatever the currently configured value for advanced options
-- Now enable the command shell
EXEC sp_configure 'xp_cmdshell', 1
--update the currently configured value with xp_cmdshell setting update.
If you are using the SSMS GUI, you may not be aware that behind the scenes, you are really issuing an ALTER DATABSE command. It is likely that another process (or processes) were accessing the database you want to take offline.
If you're a DBA, or have sysadmin privileges, issue an sp_who2 command -- looking for the ALTER DATABSE process logged to you. Kill the process.
Once the process has been killed off, issue the ALTER DATABSE command yourself.
To take the database offline:
ALTER DATABASE yourDBname
SET OFFLINE WITH ROLLBACK IMMEDIATE
To get the database back online:
ALTER DATABASE yourDBname
For added protection of data you do not wish to be online:
ALTER DATABASE yourDBname SET RESTRICTED_USER
Think of this as a lightweight alternative to SQL profiler. Under the hood of SQL profiler, there exists SQL Trace -- which provides a collection of stored procedures to generate trace info. Cut and paste the t-script below to see how it works.
-- Pay attention to what the server settings for traces look like first:
select * from sys.traces
-- you are looking to make sure there isn't already a trace file set somewhere
-- In any case you will need to create a new trace, make sure the @tracefile doesn't exist on the disk yet
declare @myTracefile nvarchar(500) set @tracefile=N'c:\temp\myTraceFile.trc'
declare @myTrace_id int
declare @maxsize bigint
set @maxsize =1
exec sp_trace_create @myTrace_id output,2,@MyTracefile ,@maxsize
--- add the result columns you care about
-- if you don't have any other traces set, you'll be set to 1, if not, run the select * sys.traces again to see
-- what got assigned. look up in sys.traces to find the @mytrace_id,
-- in this example, I will assume its 1 for now so that is why you see: @mytrace_id=1
declare @myTrace_id int
declare @xon bit
declare @current int
set @current =1
while(@current <10) -- 10 times is just an arbitrary number of times i might log something
-- pick whatever you want
-- here is where you figure out what events you want to log to the file.
-- Go here to decide: https://msdn.microsoft.com/en-us/library/ms186265.aspx
-- Try it out for now, using #14 -- that's probably not what you want, but get it to work first.
exec sp_trace_setevent @myTrace_id,14, @current,@xon
-- later you'll want to look at it
declare @myTrace_id int
exec sp_trace_setstatus @myTrace_id,1
-- see the traced event
select yourlogin, dbinstancename,* from ::fn_trace_gettable(N'myTraceFile.trc',default)
-- Once you're done, you'll need to cleanup
-- stop the trace and delete the file
declare @myTrace_id int
exec sp_trace_setstatus @myTrace_id,0
exec sp_trace_setstatus @myTrace_id,2 -- delete def from server
These general statements will get you the information that you're looking for:
WHERE TABLE_SCHEM = 'YourSchema'
AND TABLE_NAME = 'YourTableName'
This will get you the definitions of the columns:
t.table_schema as Library
,c.character_maximum_length as Length
,c.numeric_precision as Precision
,c.numeric_scale as Scale
FROM sysibm.tables t
JOIN sysibm.columns c
on t.table_schema = c.table_schema
and t.table_name = c.table_name
WHERE t.table_schema = 'YourSchema'
and t.table_name = 'YourTableName'
order by t.table_name, c.ordinal_position
Go to the server your are interested in gathering the information from, then issue this statement:
WITH LastRestoresOnServer AS
sysdb.[name] as dbName,
ROW_NUMBER() OVER (PARTITION BY sysDb.Name ORDER BY r.[restore_date] DESC) as RN
FROM master.sys.databases sysdb
LEFT OUTER JOIN msdb.dbo.[restorehistory] r ON r.[destination_database_name] = sysDb.Name
WHERE RN = 1
You were probably trying to do a comparison between 2 databases with different collation settings on a WHERE clause or on a join. Here's how you would do the comparison with a JOIN:
from source db1.schema.table1 as t1
join source db2.schema.table2 as t2
on t1.col1 collate SQL_Latin1_General_CP1_CS_AS = t2.col1 COLLATE SQL_Latin1_General_CP1_CS_AS
This pattern works well:
IF OBJECT_ID('ThisSproc', 'P') IS NOT NULL
DROP PROCEDURE ThisSproc;
CREATE PROCEDURE ThisSproc
@NewMoneyCollected MONEY = 0
SET Balance = MoneyCollectedAllYear + @NewMoneyCollected
WHERE PersonId = @PersonID;
IF @@TRANCOUNT > 0
DECLARE @ErrorNumber INT = ERROR_NUMBER();
DECLARE @ErrorLine INT = ERROR_LINE();
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE();
DECLARE @ErrorSeverity INT = ERROR_SEVERITY();
DECLARE @ErrorState INT = ERROR_STATE();
-- use as debugging tool
-- PRINT 'error number: ' + CAST(@ErrorNumber AS VARCHAR(10));
-- PRINT 'line number: ' + CAST(@ErrorLine AS VARCHAR(10));
RAISERROR(@ErrorMessage, @ErrorSeverity, @ErrorState);
The full error message reads as "Failed to deploy project. For more information, query the operation_messages view for the operation identifier '39212'. (Microsoft SQL Server, Error: 27203)
In a nutshell, the most common reason for this is the latency between SQL Server logging the deployment operation within the SSIS catalog database and the stored procs that actually commit the deployment.
Try the deployment again.
If you aren't able to install from the Help\Install New Software menu, it is probably because the zip file doesn't have the content.jar/artifacts.jar needed to do so. You will need to navigate to where you've installed Eclipse, then follow these steps:
1. Navigate to the Dropin drectory
2. Create a new subdirectory (folder).
3. Expand the zip file to the subdirectory folder.
4. Restart Eclipse
5. Under the Windows\Open Perspective menu, look for your plugin.
This usually happens when you restore a database. For example, you restore a copy of production database X to your QA server. In essence, you have overwritten the user info of that specific database with what exists in production. This creates an orphan user -- where you have no login associated with a user in that database on a server that once associated that user with the old copy of the database.
Here is what to do fix the issue:
1. Validate that what you think is the problem is the problem. Do this by listing the known orphans. You may have others show up--but be sure the id you are interested in is on the list:
use [your database instance]
EXEC sp_change_users_login 'Report'
2. Fix the broken login:
EXEC sp_change_users_login 'Auto_Fix', 'orphan username'
There are several reasons for this. The most common reason is that the account you are using while logged into mssql through ssms does not have access to the drive definition you are using as your source. Network drives are a good example of an access conflict.
Assuming you have access to the drive, try redirecting the network drive to a local drive letter via xp_cmdshell (Note: be sure use of xp_cmdshell is enabled) .
exec master..xp_cmdshell 'net use Z: "\\BackupServerXX\<share>\PathWithoutTrailingSlash" YourADpasswordHere /user:domain\your.Username'
Declare @restorefile varchar(1024) = 'Z:\thebackupfile.bak'
Restore database x
From disk = @restoreFile
When you are done, or if you've made a mistake and need to reassign the drive, delete the reference.
exec master..xp_cmdshell 'net use Z: /delete'
In SSMS, there's a Generate Script utility (read: only available under version 2008 and up) .
Here are the steps you would need to take to make use of the utility:
- Right click on the database you're interested in and go to Tasks -> Generate Scripts
- Select the tables and/or any other objects you'd like in order to get them into the script.
- Navigate to Set scripting options. Click on Advanced.
- Under the General category, navigate to Type of data to script
- Select the Schema and Data option to get the insert statements generated. Click OK.
Here's an example using forfiles to scan your storage. This command line will look for files over 200mb on the C:\ drive after 1/1/2014.
forfiles /P C:\ /M *.* /S /D +"01/01/2014" /C "cmd /c if @fsize gtr 209715200 echo @path @fsize @fdate @ftime"
For more information, check out Microsoft's examples and syntax page:http://technet.microsoft.com/en-us/library/cc753551.aspx
Try using count_big(*) instead-- this is defined as bigint instead of int.
In this example, I am looking for how many 100s of millions of rows exist by product type where each row is roughly 300 bytes wide to ultimately obtain how many gigabytes of data I need to provision.
count_big(*) as ProdCount,
(count_big(*)*300) as TotalBytes,
((count_big(*)*300)/1073741824) as Gigabytes
from fdwintegration.etl.fuelpriceindex x
inner join fdwintegration.etl.FuelProducts y
on x.ProductIndicator = y.ProductIndicator
and x.ProductType = y.ProductType
where x.ProductIndicator = 'D'
group by x.ProductType
order by x.ProductType
Click on the App Button (aka: "Drawer" ) -- That's the key with the multiple squares, click on menu , select view type, select alphabetical grid.
Open up an MSDOS command window. Type :
The id, path, user name, loc# for the file will show up in a text list. Once you find the file you want unlock, you'll need to remember the id. To unlock the file, go back to the MSDOS command window. Type:
net file 99/close (where 99 = file id)
On a windows server there are 2 other ways:
1. From an MSDOS command window type: Openfiles.exe /query /s YOUR_SERVER_NAME
2. Server Manager>Roles>File Services> Share and Storage Management (right click on SaSM) >Manage Open File