Delete top 1 rows
set rowcount 1 DELETE FROM xxx WHERE xxx set rowcount 0
2787 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)
set rowcount 1 DELETE FROM xxx WHERE xxx set rowcount 0
Exec sp_MSforeachtable "dbcc dbreindex('?')" EXEC sp_MSforeachtable 'UPDATE STATISTICS ? WITH FULLSCAN'
sp_change_users_login 'auto_fix', 'UserName'
DBCC CHECKIDENT('mytable', RESEED, 0) ;
-- The syntax for pulling a certain part of a DATETIME is: -- -- CONVERT(date_type[(length)],expression[,style]) -- -- The available styles are as follows: -- -- NULL Jun 24 2001 9:48PM -- 1 06/24/01 -- 101 06/24/2001 -- 2 01.06.24 -- 104 24.06.2001 -- 108 21:48:00 -- 112 20010624 -- 121 2001-06-24 21:48:00.000 -- Some example usage: SELECT CONVERT(DATETIME,GETDATE(),112) as date -- Will output in YYYYMMDD format SELECT CONVERT(DATETIME,client.birthday,101) as birthday -- Will output in MM/DD/YYYY format
CREATE TABLE new_table AS SELECT * FROM old_table WHERE 1 GROUP BY [COLUMN TO remove duplicates BY]; DROP TABLE old_table; RENAME TABLE new_table TO old_table;
// MySQL limit clause SELECT emp_id,lname,fname FROM employee LIMIT 20,10 // MSSQL equivelent select * from ( select top 10 emp_id,lname,fname from ( select top 30 emp_id,lname,fname from employee order by lname asc ) as newtbl order by lname desc ) as newtbl2 order by lname asc
UPDATE table SET col1 = a.col1, col2=a.col2 FROM anotherTable a WHERE a.anotherTableID = 1
// insert code here..
IF NOT EXISTS ( SELECT * FROM [information_schema].[columns] WHERE table_name = 'Customer' AND table_schema = 'dbo' AND column_name = 'FavoriteColorId' ) BEGIN ALTER TABLE [dbo].[Customer] ADD FavoriteColorId int
SELECT * FROM information_schema.columns WHERE table_name = 'Customers'
%s/DROP TABLE IF EXISTS `/DROP TABLE IF EXISTS `prefix_/g %s/CREATE TABLE `/CREATE TABLE `prefix_/g %s/INSERT INTO `/INSERT INTO `prefix_/g %s/LOCK TABLES `/LOCK TABLES `prefix_/g %s/ALTER TABLE `/ALTER TABLE `prefix_/g
delete from korovka_radacct WHERE "nasipaddress" = '81.4.195.254' AND "h323calltype" = 'VoIP';
CREATE UNIQUE INDEX comp ON logs (`site_id`, `time`);
INSERT INTO logs (`site_id`, `time`,`hits`) VALUES (1,"2004-08-09", 15) ON DUPLICATE KEY UPDATE hits=hits+15;
UPDATE [Crm].[tbPartyLegalEntityData] SET [CompanyType] = ape.[CompanyType], [EmployeesCount] = ape.[EmployeesCount] FROM [Crm].[tbPartyLegalEntityData] p INNER JOIN App.[tbApplicationPersonEmployer] AS ape ON ape.[Bulstat] = p.[Bulstat] WHERE ape.[Bulstat] <> '!' AND [ape].[EmployeesCount] IS NOT NULL AND [ape].[CompanyType] IS NOT NULL
select substring(login_id, 1, patindex('% %', login_id)) as firstname, substring(login_id, patindex('% %', login_id), len(login_id)) as surname from c_contact and login_id <> ''
// count users with filter select Count(surname+firstname) as duplicates, surname, firstname from c_contact where show_record=1 group by surname, firstname having Count(surname+firstname) > 1 order by duplicates, surname, firstname // original code SELECT column,COUNT(*) c FROM table GROUP BY column HAVING COUNT(*) > 1 //update code update c_contact set duplicates = dbo.func_get_duplicates(surname, firstname)
declare @ssql nvarchar(4000) set @ssql= ' if ''?'' not in (''tempdb'',''master'',''model'',''msdb'') begin use [?] declare @tsql nvarchar(4000) set @tsql = '''' declare @iLogFile int declare LogFiles cursor for select fileid from sysfiles where status & 0x40 = 0x40 open LogFiles fetch next from LogFiles into @iLogFile while @@fetch_status = 0 begin set @tsql = @tsql + ''DBCC SHRINKFILE(''+cast(@iLogFile as varchar(5))+'', 1) '' fetch next from LogFiles into @iLogFile end set @tsql = @tsql + '' BACKUP LOG [?] WITH TRUNCATE_ONLY '' + @tsql --print @tsql exec(@tsql) close LogFiles DEALLOCATE LogFiles end' exec sp_msforeachdb @ssql
CREATE GENERATOR testgen; SET TERM $$ ; CREATE TRIGGER trg_gen_test FOR test_table ACTIVE BEFORE INSERT AS BEGIN new.identity_column = gen_id(testgen, 1); END $$ SET TERM ; $$ SET TERM $$ ; CREATE PROCEDURE proc_insert ( val1 VARCHAR(20), val2 VARCHAR(20) ) RETURNS (generated_id INTEGER) AS BEGIN generated_id = gen_id(testgen, 1); INSERT INTO test_table (identity_column, col1, col2) VALUES (:generated_id, :val1, :val2); SUSPEND; END $$ SET TERM ; $$ /* call the above procedure like this from code (PHP): SELECT generated_id FROM proc_insert('a', 'b'); of course you can prepare and use placeholders/bind params for the proc parameters */
#!/bin/sh # This file will run a backup of your desired MySQL database and # remove any backups older than 7 days. # # If youOd like to preserve backups for longer than a week, like say # 2 weeks, then set the '-mtime' value from '+7' to '+14'. # TIME_STAMP=`date "+%Y-%m-%d"` echo "starting "$0" on" `date` for db in db1 db2 db3 do DB_STAMP=${db}_${TIME_STAMP} echo ${DB_STAMP} /opt/csw/mysql5/bin/mysqldump --opt --skip-add-locks --user=username --password=password ${db} | gzip > /domains/backups/mysql/${DB_STAMP}.gz /opt/csw/bin/mutt -s "mysql ${TIME_STAMP}" -a /domains/backups/mysql/${DB_STAMP}.gz someuser@somedomain.com dev/null done cd /domains/backups/mysql/ /usr/bin/find *.gz -mtime +14 -exec rm {} \; echo "finished "$0" on" `date`