Quantcast
Channel: Transact-SQL forum
Viewing all 12890 articles
Browse latest View live

Reg SQL server -this is my first question

$
0
0

Hi,

I am a new user .I belong to sql sever category .I know i am posting under wrong place .

But Please help me ..

create table club_values
(iccid varchar(20),
flagint
)

insert into club_values values('9098392927',1)
insert into club_values values('9098392929',1)
insert into club_values values('9098392926',1)
insert into club_values values('9098392925',1)
insert into club_values values('9098392924',1)
insert into club_values values('9098392923',1)

insert into club_values values('9098392922',0)
insert into club_values values('9098392928',0)

insert into club_values values('9098392930',1)
insert into club_values values('9098392931',1)

select * from club_values where flag =1 order by iccid

This is the table and data structure i have .

Now i need a result set like below shown in a single row(provided i have to chose flag 1 alone)

Column1      column2

9098392923 -9098392929

9098392930 - 9098392931

this means taking min and max of the series of numbers .IF there is a gap is found in the series then it should come 

as a second row

Is it possible to query this in SQL server 2008 without looping.?


Problem using "CASE" (trying convert from IIF in access)

$
0
0

I try convert a function IIF from ACCESS, replacing "IIF" for a "CASE", and get me a error in "SELECT line 19", can any1 help me ?

-----------------

PROCEDURE  [GetInfo_ASN] 
@P1 smalldatetime     -- Data de Entrada
AS
BEGIN
  SET NOCOUNT ON;
SELECT 
  dados.dbo.GVAL_PEDIDOS.AVAP_Id AS [Reg SIGA], 
  dados.dbo.GVAL_BEM.AVAB_Codigo AS [Cod Bem], 
  select case when (SUBSTRING([dados.dbo.AVAB_Codigo],5,1)="0")
              then Right([dados.dbo.AVAB_Codigo],6)
 else (SUBSTRING([dados.dbo.AVAB_Codigo],5,1) & Right([dados.dbo.AVAB_Codigo],6))
         end as [UE/UL],
  dados.dbo.GVAL_PEDIDOS.AVAP_dtEnvio AS [Dt Envio], 
  dados.dbo._FREGUESAS.F_Distrito AS Distrito, 
  dados.dbo._FREGUESAS.F_Concelho AS Concelho
FROM (dados.dbo.GVAL_PEDIDOS INNER JOIN dados.dbo.GVAL_BEM 
ON GVAL_PEDIDOS.AVAP_Codigo = GVAL_BEM.AVAB_Codigo) INNER JOIN _FREGUESAS 
ON GVAL_BEM.AVAB_DCF = _FREGUESAS.F_Codigo
WHERE (((GVAL_PEDIDOS.AVAP_dtEnvio)>=@P1) 
      AND ((GVAL_PEDIDOS.AVAP_Status)=7) 
      AND ((GVAL_PEDIDOS.AVAP_Origem)='7521'));

error when try a "cast" in a char(13) to get a varchar....

$
0
0

AVAB_Codigo it's a "char(13)", get me a error :

"Invalid operator for data type. Operator equals boolean AND, type equals varchar."

when i use a cast...  the error still appear,.....  what should i do ?

-- 1st attemp --------------------------------------------------------------------

  case when (SUBSTRING(AVAB_Codigo,5,1)='0') 
   then Right(AVAB_Codigo,6) 
   else (SUBSTRING(AVAB_Codigo,5,1) & Right(AVAB_Codigo,6)) 
  end as [UL/UE]

-- 2nd attempt ----------------------------------------------------------------------

  case when (SUBSTRING(cast(AVAB_Codigo as varchar(10)),5,1)='0') 
   then Right(cast(AVAB_Codigo as varchar(10)),6) 
   else (SUBSTRING(cast(AVAB_Codigo as varchar(10)),5,1) & Right(cast(AVAB_Codigo as varchar(10)),6)) 
  end as [UL/UE],

SQL Server Management Studio experiences lock timeout when using SELECT INTO

$
0
0
Whenever I execute a long-running SELECT INTO statement and while it is running I try to use the SQL Server Management Studio object browser to view tables or other objects, I get a lock timeout error. Is there anyway to stop this from happening? Is there any specific syntax I can use with the SELECT INTO to stop it from locking up the system tables?

Computed Column Adding 3 columns together

$
0
0

Hi



I am trying to create a computed column in a SQL table to show 3 columns in the same table joined together to make a long string.



I currently have the following:



CREATE

TABLE[dbo].[Item_Details1](

[Product_Description][nvarchar](255)NOTNULL,

[Manufacturer_Part_No][nvarchar](255)NULL,

[Preferred_Supplier][nvarchar](255)NOTNULL,

[Srch_Field]As[Product_Description]+[Preferred_Supplier]+[Manufacturer_Part_No]



I have also tried:



CONCAT([Product_Description],[Preferred_Supplier],[Manufacturer_Part_No])



CAST([Product_Description] as NVARCHAR(MAX)) & CAST([Preferred_Supplier] as NVARCHAR(MAX))& CAST([Manufacturer_Part_No]) as NVARCHAR(MAX))



[Product_Description]&[Preferred_Supplier]&[Manufacturer_Part_No]



See what I am trying to do next once the table is Linked in Access is to search this field and filter based on the content as supplied in a search text box.



Any Ideas?



how to save the result set of a SELECT in a variable ?

$
0
0

declare aVal int, bVal int;

select min(d.DateValue) begindate, max(d.DateValue) enddate from dbo.dimDate d

where d.BusinessYear = 2014 

what I want to do is to save the result of the select statement in aVal and bVal 

so aVal=begindate and bVal=enddate

Select query for grouping

$
0
0

I need to create email and send it with all channels together the users are subscribed to,
This is my source table:

CREATE TABLE [dbo].[test_email]([email] [varchar](50) NOT NULL,[channel_id] [int] NOT NULL,CONSTRAINT [PK_test_email] PRIMARY KEY CLUSTERED ([email],[channel_id]))

INSERT INTO test_email(email, channel_id)
	VALUES('email@gmail.com', 2), ('email@gmail.com', 15),
	('email1@gmail.com', 2),
	('email2@gmail.com', 2), ('email2@gmail.com', 15), ('email2@gmail.com', 19),
	('email3@gmail.com', 2), ('email3@gmail.com', 15)

For channel_id=2, there is only user with email: 'email1@gmail.com'

For channel_id in(2,15) there are 2 users: 'email@gmail.com' and 'email3@gmail.com'.

For channel_id in(2,15,19) there is user with email:'email2@gmail.com'

And so on...

The result should be(groupID is just for sake of clarity):

groupID   emails                                                          channel_id                                                    
------------------------------------------------------------------------------------

1              email@gmail.com, email3@gmail.com                 2
1              email@gmail.com, email3@gmail.com                 15
2              email1@gmail.com                                              2
3              email2@gmail.com                                              2
3              email2@gmail.com                                              15
3              email2@gmail.com                                              19

Emails column could have up to 50 emails. Maybe I should create some groupID table and move emails to separate table.

Bringing back a value that meets a criteria.

$
0
0

Hi,

How do i bring back all the fields that only meet the fallowing criteria: Field4 is all NULL. So i would get back all the fields and rows for value (82930017). Thank you for your help

Field1Field2Field3Field4
3260587235    000016518 NULL
260587235    000016518NULL
4060587235    00001651813123050
3360587235    00001651813147254
3560587235    00001651813121490
3960587235    00001651813124123
3660587235    00001651813126715
160587235    00001651813124850
3760587235    000016518NULL
3860587235    00001651813127555
3682930017    000037845NULL
3482930017    000037845NULL
3282930017    000037845NULL
3782930017    000037845NULL
282930017    000037845NULL
3382930017    000037845NULL
4082930017    000037845NULL
3882930017    000037845NULL
282930017    000037845NULL
3582930017    000037845NULL
3182930017    000037845NULL
3982930017    000037845NULL
182930017    000037845NULL


Edwin Lopera



SQLServer 2008 R2: The SCHEMA LOCK permission was denied on the object

$
0
0

Hi all,

I encounter the following error while developing a SSRS project that connects to a SQL Server Database view:

"Msg 229, Level 14, State 71, Procedure sp_getschemalock, Line 1
The SCHEMA LOCK permission was denied on the object 'Table4', database 'DBRemote', schema 'dbo'."

That view uses a linked server to select data from a remote SQL Server Database (SQL Server 2005).

There are no sql hints specified in my views

My view T-SQL is:

Select

...

From linksv.DBRemote.dbo.Table1 T1

Inner Join linksv.DBRemote.dbo.Table2 T2 On

  T1.fk1 = T2.pk1

Inner Join view1 v1 On

  T2.fk2 = v1.pk2

 

My t-sql for view1 is:

Select

...

From linksv.DBRemote.dbo.Table3 T3 

Inner Join linksv.DBRemote.dbo.Table4 T4 On

  t3.fk1 = T4.pk1

 

The object specified in error message above refers to Table "linksv.DBRemote.dbo.Table4" (see view above)

 

SQL Server Permissions are set for all objects involved in the queries above.

 

The funny thing is that the error occurs when I run my report from the report server webinterface

and my report project is loaded in BIDS at the same time.

The error occurs when I execute the query in SSMS 2008 and also when I run the query

in BIDS 2008 Query designer.

 

I also wondering why the error referes to the "linksv.DBRemote.dbo.Table4" remote object only

but not to the other remote objects in that query.

 

Im not sure where to look any further on what might cause this error.

Appreciate any help very much.

Thanks

Bodo





formulating a count query - sql server 2012

$
0
0

Hello,

I am quite new to SQL and even though I can handle simple queries I have run into a problem. Maybe you can help :-)

This is how the table I want to query from is structured:

   id  From                                 To                          Anomalies         

   1         01-08-2014 00:00:00       01-08-2014 00:30:00        24

   2      01-08-2014 00:00:00       01-08-2014 00:30:00 80

   3         01-08-2014 00:00:00       01-08-2014 00:30:00        11

   4         01-08-2014 00:00:00       01-08-2014 00:30:00        10

   5         01-08-2014 00:00:00       01-08-2014 00:30:00        90

   1         01-08-2014 00:30:00       01-08-2014 01:00:00        20

   2         01-08-2014 00:30:00       01-08-2014 01:00:00        74

   3         01-08-2014 00:30:00       01-08-2014 01:00:00        11

   4         01-08-2014 00:30:00       01-08-2014 01:00:00        100

   5         01-08-2014 00:30:00       01-08-2014 01:00:00         98

..........................................................................................................................

The problem is to write a query that will return the number of anomalies per department for the month of, say, July 2014. The final result set should have 5 records.

Does anyone have any ideas?

Thx

Help with query

$
0
0

Hi,

I have a table which has columns that records the logon and logoff times of the users every thime they do so. here are the column descriptions

MSG DATE - DATE of the Logon\logoff

MSG TIME - TIME of the logon\log off

Log Type -  Type of activity ( Logon or Logoff.)

User NAme

Q)My requirement is to find who has logged on in a period of time and last login time when supplied with username Can you please help?

Many Thanks,

Bhanu

Validating different DATE FORMATs

$
0
0

Hi Friends...

I get in trouble again, I have different flat files with a DATE column in string format, but, the problem is that each file has a different DATE FORMAT in that column, for example:  yyyy/mm/dd , yyyy/dd/mm , dd/mm/yyyy , mm/dd/yyyy  . . . and i need to put these data in a table without changing the original DATE. Today is 2014/08/03 or August 3rd 2014, i need to write this data in the table without changing it to 2014/03/08 or March 8th 2014.

Can you give me any idea to solve this problem ?

I have one, but im not sure if it is the best option, while the DTSx is uploading the data to the stage table, it tries to identify what is the DATE FORMAT using a script and using the first two rows, because the 1st row containt the day 1 and the second row the day 2, always, its sorted by date.

Thanks in advance, regards !


Sergio Sánchez Arias
Oaxaca,México
AYÚDANOS A AYUDARTE


Use Pivot & Unpivot in SQL

$
0
0

Dear All,

I have a data under Table in below shape..


1 - means this user is setup on that feature, & 0 means not.


But, I want to see this output in that way..


Means - If multiple e-mails are configure on same feature, it should come with semi colon in pivot mode..

Pls Help..


Need help to delete DB instance in 2008 R2 Server

$
0
0

Hi

The help click link didnt help me trying to solve my problem in removing a database instance in MS SQL Server Management Studio of Server 2008 R2

Is there a solution to this ?

 

TITLE: Microsoft SQL Server Management Studio
------------------------------

Delete backup history failed for Server 'localhost\SQLEXPRESS'.  (Microsoft.SqlServer.Smo)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=10.50.1600.1+((KJ_RTM).100402-1539+)&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Delete+backup+history+Server&LinkId=20476

------------------------------
ADDITIONAL INFORMATION:

An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)

------------------------------

The EXECUTE permission was denied on the object 'sp_delete_database_backuphistory', database 'msdb', schema 'dbo'. (Microsoft SQL Server, Error: 229)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=09.00.4053&EvtSrc=MSSQLServer&EvtID=229&LinkId=20476

------------------------------
BUTTONS:

OK
------------------------------

Dynamic Function

$
0
0
I want to write a function, which accept 3 parameters, 1 TableName 2 ColumnName 3 DateValue,
and returns number of records in that table for that particular date(in parameter date), 
I have written below function but it is not returning the desired result, can anyone help me.

CREATE FUNCTION dbo.[f_Rec_cnt] 
(@InTableName NVARCHAR(100),
@InDtColName NVARCHAR(50),
@InDate NVARCHAR(50)
        )
RETURNS INT
AS
BEGIN
DECLARE @vRowCnt NVARCHAR(50)
DECLARE @vTableName NVARCHAR(150)
DECLARE @vDtColName NVARCHAR(50)
DECLARE @vInDate NVARCHAR(50)
DECLARE @vSql NVARCHAR(500)
SELECT @vTableName=@InTableName,@vDtColName=@InDtColName,@vInDate=@InDate
      SELECT @vInDate=''''+CAST(@InDate AS NVARCHAR)+''''
      SELECT @vSql='SELECT @vRowCnt = COUNT(*) FROM '+@vTableName+' WHERE '+@vDtColName+'='+@vInDate
      EXECUTE SP_EXECUTESQL @vSql
      RETURN (CAST(@vRowCnt AS INT))
END


Automatic Refresh Local Cache? (SSMS IntelliSense)

What different between these database designs in term of performance and HD allocation?

$
0
0

I have designed the database about sale order and i realize that these two designs provided me the same result.

Could you tell me that What different between these database designs in term of performance and  HD allocation?


 

Bringing back specific rows.

$
0
0

Hi SQL Gurus,

The first 3 columns are nvarchar(500) and the last one is a column i generated with a LEN function so its an INT and can have NULLs or 0. Below is the sample.  Field2 will be grouped together and if any of its corresponding field3 values do not match the rest of the Field2 values i need that row back. Thank you for any help.

Results
Results
Field1Field2Field3Field4
2Heading10010Material                                                 NylNULL
5Heading10013Jkyng0
5Heading10013Jklty0
5Heading10013Jkrty0
4Heading10012LenTTNULL
4Heading10012LenEENULL




DATA SET: Field1 -3 are nvarchar(500) and Field is an INT

Field1 Field2 Field3 Field4
1Heading 10009Type                                                                        Prim 124
1Heading 10009Type                                                                        Acc 54
1Heading 10009Type                                                                        Act 64
1Heading 10009Type                                                                        Deb 157
2Heading 10010Material                                                  Talc 94
2Heading 10010Material                                                  Nyl NULL
2Heading 10010Material                                                  Cub 249
2Heading 10010Material                                                  Alm 220
2Heading 10010Material                                                  Zirc 288
2Heading 10010Material                                                  Cer 196
2Heading 10010Material                                                  Sil 112
3Heading 10011Cut                                                                Fin NULL
3Heading 10011Cut                                                                Vry NULL
3Heading 10011Cut                                                                Crs NULL
3Heading 10011Cut                                                                Extr NULL
3Heading 10011Cut                                                                Md NULL
4Heading 10012Len TT NULL
4Heading 10012Len EE NULL
4Heading 10012Len GG 12
5Heading 10013Jk yng 0
5Heading 10013Jk lty 0
5Heading 10013Jk rty 0
5Heading 10013Jk rtu 12

Edwin Lopera



Sum Query From 6 Tables

$
0
0

I need to in SQL if possible sum the total from 6 different stores, who are all stored in a different database.  I would prefer not to use a make table (again if possible) would just like a direct query using maybe CTE?

--Query 1
Select sum(emp1Sales+emp2Sales+emp3Sales+man1Sales+man2Sales)
From store1
--Query 2
Select sum(emp1Sales+emp2Sales+emp3Sales+man1Sales+man2Sales)
From store2
--Query 3
Select sum(emp1Sales+emp2Sales+emp3Sales+man1Sales+man2Sales)
From store3
...and so on.

How can I Sum all of those sales in one query?  So instead of having 3 values returned have one query that returns all the values?

Single Row Values

$
0
0

Hi All ,

I have a table with below output

Value1 Value2 Value3 ID
Department NULL NULL 97100
Type NULL NULL 63957
Department NULL NULL 177164
NULL State  NULL 97100
NULL Center NULL 63957
NULL Center NULL 177164
NULL NULL Global 97100

I require the result in the below format kindly help via set based query

Value1 Value2 Value3 ID
Department State Global 97100
Type Center Null 63957
Department Center Null 177164

Value1 Value2 Value3 ID




























Value1 Value2 Value3 ID












thanks

Priya


Viewing all 12890 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>