How To Write If Else Statement With Variable For A Table Name

Apr 3, 2013

I am running SQL Server 2000 and need to know how to write an IF else statement with a variable for a table name. I am constantly getting errors when I attempt this feat.

Code:
Use [TestDatabase]
Go
CREATE PROCEDURE UserInputAsTable

[code]....

View 14 Replies


ADVERTISEMENT

How To Write A SELECT Statement And Store The Result In A Session Variable

Nov 6, 2007

I'm sure this is a very simple piece of code, but I'm having trouble understanding how to do this.
First I have a database with three columns


ContactID

View 1 Replies View Related

How To Write SQL To Form New Table From Job Revence And Job Cost Using SQL Statement ? Thx

Dec 7, 2004

Edited by SomeNewKid. Please post code between <code> and </code> tags.


// Pls copy the following HTML after that you generate the HTML page. Then you will know what I mean. I need to form "New Table" from Job Revence and Job Cost using SQL statement? thx



-----------The following code-----------------

<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=big5">
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=Big5">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 9">
<link rel=File-List href="cid:filelist.xml@01C4DA4E.7D08F5E0">
<!--[if !mso]>
<style>
v:* {behavior:url(#default#VML);}
o:* {behavior:url(#default#VML);}
x:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
<style>
<!--table
{mso-displayed-decimal-separator:".";
mso-displayed-thousand-separator:",";}
.xl15
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:????;
mso-generic-font-family:auto;
mso-font-charset:136;
mso-number-format:General;
text-align:general;
vertical-align:bottom;
mso-background-source:auto;
mso-pattern:auto;
white-space:nowrap;}
.xl22
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:????;
mso-generic-font-family:auto;
mso-font-charset:136;
mso-number-format:General;
text-align:general;
vertical-align:bottom;
border:.5pt solid windowtext;
mso-background-source:auto;
mso-pattern:auto;
white-space:nowrap;}
.xl23
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:????;
mso-generic-font-family:auto;
mso-font-charset:136;
mso-number-format:General;
text-align:general;
vertical-align:bottom;
border:.5pt solid windowtext;
background:#339966;
mso-pattern:auto none;
white-space:nowrap;}
.xl24
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:12.0pt;
font-weight:700;
font-style:normal;
text-decoration:none;
font-family:????, serif;
mso-font-charset:136;
mso-number-format:General;
text-align:general;
vertical-align:bottom;
mso-background-source:auto;
mso-pattern:auto;
white-space:nowrap;}
-->
</style>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>Sheet1</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>225</x:DefaultRowHeight>
<x:Print>
<x:ValidPrinterInfo/>
<x:PaperSizeIndex>9</x:PaperSizeIndex>
<x:HorizontalResolution>-3</x:HorizontalResolution>
<x:VerticalResolution>0</x:VerticalResolution>
</x:Print>
<x:Selected/>
<x:Panes>
<x:Pane>
<x:Number>3</x:Number>
<x:ActiveCol>10</x:ActiveCol>
</x:Pane>
</x:Panes>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
<x:WindowHeight>10470</x:WindowHeight>
<x:WindowWidth>16755</x:WindowWidth>
<x:WindowTopX>240</x:WindowTopX>
<x:WindowTopY>30</x:WindowTopY>
<x:HasEnvelope/>
<x:ProtectStructure>False</x:ProtectStructure>
<x:ProtectWindows>False</x:ProtectWindows>
</x:ExcelWorkbook>
</xml><![endif]-->
</head>

<body>

<table x:str border=0 cellpadding=0 cellspacing=0 width=636 style='border-collapse:
collapse;table-layout:fixed;width:477pt'>
<col width=56 span=7 style='width:42pt'>
<col width=104 style='mso-width-source:userset;mso-width-alt:4437;width:78pt'>
<col width=140 style='mso-width-source:userset;mso-width-alt:5973;width:105pt'>
<tr height=22 style='height:16.5pt'>
<td height=22 class=xl24 colspan=2 width=112 style='height:16.5pt;mso-ignore:
colspan;width:84pt'>JOB TABLE</td>
<td class=xl15 width=56 style='width:42pt'></td>
<td class=xl15 width=56 style='width:42pt'></td>
<td class=xl15 width=56 style='width:42pt'></td>
<td class=xl15 width=56 style='width:42pt'></td>
<td class=xl15 width=56 style='width:42pt'></td>
<td class=xl15 width=104 style='width:78pt'></td>
<td class=xl15 width=140 style='width:105pt'></td>
</tr>
<tr height=15 style='height:11.25pt'>
<td height=15 class=xl23 style='height:11.25pt'>JobNo</td>
<td colspan=8 class=xl15 style='mso-ignore:colspan'></td>
</tr>
<tr height=15 style='height:11.25pt'>
<td height=15 class=xl22 style='height:11.25pt;border-top:none'>SE0001</td>
<td colspan=8 class=xl15 style='mso-ignore:colspan'></td>
</tr>
<tr height=22 style='height:16.5pt'>
<td height=22 class=xl22 style='height:16.5pt;border-top:none'>SE0002</td>
<td colspan=5 class=xl15 style='mso-ignore:colspan'></td>
<td class=xl24 colspan=2 style='mso-ignore:colspan'>New Table</td>
<td class=xl15></td>
</tr>
<tr height=15 style='height:11.25pt'>
<td height=15 colspan=6 class=xl15 style='height:11.25pt;mso-ignore:colspan'></td>
<td class=xl23>JobNo</td>
<td class=xl23 style='border-left:none'>no of Revence</td>
<td class=xl23 style='border-left:none'>no of Cost</td>
</tr>
<tr height=22 style='height:16.5pt'>
<td height=22 class=xl24 colspan=2 style='height:16.5pt;mso-ignore:colspan'>JOB
Revence</td>
<td colspan=4 class=xl15 style='mso-ignore:colspan'></td>
<td class=xl22 style='border-top:none'>SE0001</td>
<td class=xl22 align=right style='border-top:none;border-left:none' x:num>2</td>
<td class=xl22 align=right style='border-top:none;border-left:none' x:num>1</td>
</tr>
<tr height=15 style='height:11.25pt'>
<td height=15 class=xl23 style='height:11.25pt'>JobNo</td>
<td class=xl23 style='border-left:none'>ItemNo</td>
<td colspan=4 class=xl15 style='mso-ignore:colspan'></td>
<td class=xl22 style='border-top:none'>SE0002</td>
<td class=xl22 align=right style='border-top:none;border-left:none' x:num>1</td>
<td class=xl22 align=right style='border-top:none;border-left:none' x:num>0</td>
</tr>
<tr height=15 style='height:11.25pt'>
<td height=15 class=xl22 style='height:11.25pt;border-top:none'>SE0001</td>
<td class=xl22 align=right style='border-top:none;border-left:none' x:num>1</td>
<td colspan=7 class=xl15 style='mso-ignore:colspan'></td>
</tr>
<tr height=15 style='height:11.25pt'>
<td height=15 class=xl22 style='height:11.25pt;border-top:none'>SE0001</td>
<td class=xl22 align=right style='border-top:none;border-left:none' x:num>2</td>
<td colspan=7 class=xl15 style='mso-ignore:colspan'></td>
</tr>
<tr height=15 style='height:11.25pt'>
<td height=15 class=xl22 style='height:11.25pt;border-top:none'>SE0002</td>
<td class=xl22 align=right style='border-top:none;border-left:none' x:num>1</td>
<td colspan=7 class=xl15 style='mso-ignore:colspan'></td>
</tr>
<tr height=15 style='height:11.25pt'>
<td height=15 colspan=9 class=xl15 style='height:11.25pt;mso-ignore:colspan'></td>
</tr>
<tr height=22 style='height:16.5pt'>
<td height=22 class=xl24 colspan=2 style='height:16.5pt;mso-ignore:colspan'>JOB
Cost</td>
<td colspan=7 class=xl15 style='mso-ignore:colspan'></td>
</tr>
<tr height=15 style='height:11.25pt'>
<td height=15 class=xl23 style='height:11.25pt'>JobNo</td>
<td class=xl23 style='border-left:none'>ItemNo</td>
<td colspan=7 class=xl15 style='mso-ignore:colspan'></td>
</tr>
<tr height=15 style='height:11.25pt'>
<td height=15 class=xl22 style='height:11.25pt;border-top:none'>SE0001</td>
<td class=xl22 align=right style='border-top:none;border-left:none' x:num>1</td>
<td colspan=7 class=xl15 style='mso-ignore:colspan'></td>
</tr>
<![if supportMisalignedColumns]>
<tr height=0 style='display:none'>
<td width=56 style='width:42pt'></td>
<td width=56 style='width:42pt'></td>
<td width=56 style='width:42pt'></td>
<td width=56 style='width:42pt'></td>
<td width=56 style='width:42pt'></td>
<td width=56 style='width:42pt'></td>
<td width=56 style='width:42pt'></td>
<td width=104 style='width:78pt'></td>
<td width=140 style='width:105pt'></td>
</tr>
<![endif]>
</table>

</body>

</html>

View 2 Replies View Related

Use A Table Name As A Variable In The From Statement

Oct 26, 2005

I'm curious if anyone knows the correct way to do this pseudo-statement correctly?  I want to create a stored procedure in which I send it the table name of the table I want to query.declare @tableName varchar(500)set @tableName = 'PortfolioPreferenceOwnership' select * from @tableName

View 4 Replies View Related

Variable For The Table Name In A SELECT Statement.

Apr 23, 2007

Hi,I'm trying to dynamically assign the table name for a SELECT statement but can't get it to work. Given below is my code: SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE GetLastProjectNumber (@DeptCode varchar(20))
AS
BEGIN TRANSACTION
SET NOCOUNT ON

DECLARE @ProjectNumber int
SET @ProjectNumber = 'ProjectNumber' + REPLACE(CONVERT(char,@DeptCode),'.','')
SELECT MAX(@ProjectNumber)
FROM 'tbl_ProjectNumber' + REPLACE(CONVERT(char,@DeptCode),'.','');
END TRANSACTION  Basically, I have a bunch of tables which were created dynamically using the code from this post and now I need to access the last row in a table that matches the supplied DeptCode. This is the error I get:Msg 102, Level 15, State 1, Procedure GetLastProjectNumber, Line 29Incorrect syntax near 'tbl_ProjectNumber'. Any help would be appreciated.Thanks. 

View 3 Replies View Related

Using A Variable To Specify Table Column In Sql Statement??

Feb 6, 2008

Hello everyone,
I'm still quite new ASP.net, Visual Web Developer 2008, and SQL. It has been a fun learning experience so far. Anyways, the site I am designing needs to allow its users to extensively search several different databases (MS SQL databases).  I have followed many of the tutorials and have found it rather easy to add table adapters, gridviews and other data features that use basic SQL Select statements. One of the major database tables contains several columns which I would like to include as a search parameters from a drop down list. I was wondering if there is any way I can write a select which will pass a variable to be used as a column name to the statement?  For example:
SELECT DATE, GAME, EXACT, @COLNAMEFROM HistoryWHERE @COLNAME = @SOMEVARIABLE
This obviously doesnt work, but thats the gist of what I want to do. Any suggestions? I need this to be simple as possible, Most everything I'm doing is done through the visual design mode. Im still slow to learn C# and apply it in the codebehind files unless I have very detailed step by step instuctions.
Thanks
Scott

View 15 Replies View Related

Table Name In Variable - Create Statement

Jul 20, 2005

Is it possible to have part of a table name used in a CREATE statementcontained in a variable? Here's what I'd like to do, althoughobviously the syntax of this isn't quite right or I wouldn't be hereasking:DECLARE @TblPrefix char (3)SET @TblPrefix = 'tst'CREATE TABLE @TblPrefix + TestTable (col1 int)The point there is to have a table named tstTestTableThe reason I need to do this is that my ISP will only give me onedatabase to work with and I'd like to have two copies of theapplication I'm developing running at the same time. So I'd like torun the sql script that creates the tables with TblPrefix set to "dev"and then run it again with TblPrefix set to "liv"thankseric

View 3 Replies View Related

Can't TABLE Variable Be Used In EXEC Statement In Stored Procedure

Feb 8, 2008

Hi

I've used a temporary table in the stored procedure

I've created it as DECLARE @Temp TABLE (id INT)

in select clause I've used this temp table through the joins..

But I've also used a varchar variable to store my criteria...

which I'm building in the stored procedure

so inorder to use it in the where clause I used

EXEC ('SELECT ....................


FROM @Temp T

LEFT OUTER JOIN ...
LEFT OUTER JOIN ...
WHERE ' + @Criteria )


Unfortunately this is not working.
Giving the errror


Must declare the variable '@TempT'.

I had to use Temporary table using #, which I feel is really waste of memory...

Is there a way by which I can use my Table variable in the EXEC statement.

Thanks In advance

View 4 Replies View Related

Random Selection From Table Variable In Subquery As A Column In Select Statement

Nov 7, 2007

Consider the below code: I am trying to find a way so that my select statement (which will actually be used to insert records) can randomly place values in the Source and Type columns that it selects from a list which in this case is records in a table variable. I dont really want to perform the insert inside a loop since the production version will work with millions of records. Anyone have any suggestions of how to change the subqueries that constitute these columns so that they are randomized?




SET NOCOUNT ON


Declare @RandomRecordCount as int, @Counter as int
Select @RandomRecordCount = 1000

Declare @Type table (Name nvarchar(200) NOT NULL)
Declare @Source table (Name nvarchar(200) NOT NULL)
Declare @Users table (Name nvarchar(200) NOT NULL)
Declare @NumericBase table (Number int not null)

Set @Counter = 0

while @Counter < @RandomRecordCount
begin
Insert into @NumericBase(Number)Values(@Counter)
set @Counter = @Counter + 1
end


Insert into @Type(Name)
Select 'Type: Buick' UNION ALL
Select 'Type: Cadillac' UNION ALL
Select 'Type: Chevrolet' UNION ALL
Select 'Type: GMC'

Insert into @Source(Name)
Select 'Source: Japan' UNION ALL
Select 'Source: China' UNION ALL
Select 'Source: Spain' UNION ALL
Select 'Source: India' UNION ALL
Select 'Source: USA'

Insert into @Users(Name)
Select 'keith' UNION ALL
Select 'kevin' UNION ALL
Select 'chris' UNION ALL
Select 'chad' UNION ALL
Select 'brian'


select
1 ProviderId, -- static value
'' Identifier,
'' ClassificationCode,
(select TOP 1 Name from @Source order by newid()) Source,
(select TOP 1 Name from @Type order by newid()) Type

from @NumericBase



SET NOCOUNT OFF

View 14 Replies View Related

How To Write Select Statement Inside CASE Statement ?

Jul 4, 2006

Hello friends,
I want to use select statement in a CASE inside procedure.
can I do it? of yes then how can i do it ?

following part of the procedure clears my requirement.

SELECT E.EmployeeID,
CASE E.EmployeeType
WHEN 1 THEN
select * from Tbl1
WHEN 2 THEN
select * from Tbl2
WHEN 3 THEN
select * from Tbl3
END
FROM EMPLOYEE E

can any one help me in this?
please give me a sample query.

Thanks and Regards,
Kiran Suthar

View 7 Replies View Related

How To Write A SP With Null If Variable Is Absent?

Dec 13, 2006

I have a Store Procedure that kept wanting to specify something like maidenName = null if there is not value for maidenName field. The maidenName is already set up to allow null value. How do I write the store procedure to automaticaly recognized that if a field has nothing in it's a null? Below is the error I kept getting if I do not have the expiration number.
Msg 201, Level 16, State 4, Procedure spInsertName, Line 0
Procedure 'spInsertName' expects parameter '@expNo', which was not supplied.

View 3 Replies View Related

Need To Write Value Of Session Variable To SQL Record

Jan 8, 2006

Each user who inserts a new SQL record from the FormView control needs to have their UserID in one of the fields of the record.  I have the user ID stored in the Session("UserID") variable.  I am having trouble finding the right way to get this done.  I have tried using a hidden text box but I can't seem to assign the value.  I have tried the Insert Parameters but that will not accept <%# Session("UserID") %> as a DefaultValue.  Any ideas would be helpful.  Thanks.

View 5 Replies View Related

Read Write Same Variable In Script Task

Nov 1, 2007

I want to write a variable say testVar in PostExecute of my script. I also want to read the same variable at the start of my script block.

How to specify this variable in the script transformation editor

ReadOnlyVariables = ????
WriteOnlyVariables = ???

If i just define

WriteOnlyVariables = testVar

will it work . I mean i cannot use testVar in both read & write


Any suggestions...


Thanks

View 3 Replies View Related

Write Value From Script Component To Variable (Datatype Object)

May 8, 2008

Hi,

I have a script component that contains a script similar to that shown below;


Public Sub Main()

Dim sqlConn As New SqlConnection("Server=myserver;Database=mydb;Trusted_Connection=True;")

Dim SQLStmt As String

SQLStmt = "select top 10 CountID from mytable"


Dim sqlComm As New SqlCommand(SQLStmt, sqlConn)


sqlConn.Open()



Dim r As SqlDataReader = sqlComm.ExecuteReader()
While r.Read()


Dim MyValue As Integer

If r("CountID") IsNot DBNull.Value Then

MyValue = CInt(r("CountID "))



' HERE I WANT TO WRITE MyValue TO A PACKAGE VARIABLE AS A ROW

End If


End While

r.Close()
sqlConn.Close()

Dts.TaskResult = Dts.Results.Success

End Sub


Once i have a value assigned to MyValue, I want to write this as a row to a variable (DATATYPE Object Value VALUE System.Object) in the package which will be an ADO Recordset Destination that i can then loop through later.

Many thanks for any help

Phil Harbour

View 1 Replies View Related

Want To Write A Query Which Select The Columns Passed As Variable In Sql Sp

May 2, 2008

i want to select the values of column passed from the user like


connectionsizein or connectionsizemm

how can i do

i am using sql server 2005

View 2 Replies View Related

How Do I Write This SQL Statement?

Sep 11, 2007

I have a table named "products", it has a column called "category", I have data such as "CategoryA", "CategoryB" and "CategoryC" etc.... now I want to do a select statement and choose "CategoryA" and "CategoryB" but I want to rename them as "A" and "B". how do I write such a statement... thanks!
 

View 9 Replies View Related

How To Write SQL Statement

Feb 8, 2008

I wanted  to select few columns from the result of calling this store procedure and sort by a column.
{CALL FIQR.Portfolio.Loans_In_Portfolio('2007-09-07', 'FISYND', '', '%')}
How should I write it?(without changing the store procedure)
 

View 8 Replies View Related

How To Write Sql Statement

Apr 29, 2005

i have those columns: id, no, description.

View 3 Replies View Related

Help.. How To Write This Sql Statement?

Dec 14, 2006

Hi all,

DECLARE @STA as char(50)

SET @STA = 'TT'

SELECT * FROM ABC_TBL where ABC_Filed in (@STA)

if i have multivalue in parameter @STA, like 'TT', 'DD', 'AA'

so how can i write my statement? i only need to pass one parameter with multilist, so do anybody know how to write the MS sql statement?

Example like

DECLARE @STA as char(50)

SET @STA = 'TT', 'DD'

SELECT * FROM ABC_TBL where ABC_Filed in (@STA)

Thank You.

Regards,
S3ng

View 3 Replies View Related

Trying To Write A Select Statement.

Jul 26, 2007

I want to write a select statement that will show only distinct Student IDs where every record in the table with that student id has a grade above 80?
Lets say for example the table looks like this:




Student_ID

Grade


11276

93


56879

81


11276

76


44327

92


11276

84


56879

97


56879

82 The select statement should only show students, 56879 & 44327 because student 11276 made a 76 in one class.Anyone know how to word this select statement?Thanks for any help!

View 8 Replies View Related

How To Write A Sql Statement To Get The First Needed Row?

May 9, 2007

I have a table with two columns. empnum and empname. there are some data with same empnum but different empname. I just want to get the first empname for the same empnum. Is there a way to write a sql stetment to do this?



Thanks

View 3 Replies View Related

Is It Possible Write In 1 Query Statement?

Aug 20, 2007

Hi guys



I'll appreciate any help with the following problem:

Table 1: TBSALESHEADER
PK: SALESNO

Table 2: TBSALESENTRY

FK: SALESNO


This query is return 2 rows.
SELECT PAYMTDID AS TYPE, CONVERT(CHAR(8),SALESDATE,112) AS DATE, SUM(SALESAMT) AS AMOUNT FROM TBSALESHEADER A, TBSALESENTRY B WHERE A.SALESNO = B.SALESNO AND CONVERT(CHAR(8),SALESDATE,112) = '20070701'
GROUP BY PAYMTDID,CONVERT(CHAR(8),SALESDATE,112)


TYPE DATE AMOUNT
----------------------------------------------------------
0 20070701 3041.29
1 20070701 1442.20


Issue:
Instead of retrieving 2 rows showing 2 type of records, I want to display the 2 type in 2 columns (need to return in 1 row).


Can I get this desired result as following in 1 query statement:

DATE AMOUNT (TYPE 1) AMOUNT (TYPE 2)
---------------------------------------------------------------------------------
20070701 3041.29 1442.20

Please help.

Thanks.

View 5 Replies View Related

Is It Possible To Write If/Then Else Within SQL Select Statement For Vb.net?

Aug 1, 2007

I'm running a select statement from within my VB.net code. However, if the software package name that I chose from a drop down list in my form is not available in the search (in the TableColumn called Software.Package_Name), I would like to be able to write the message "Record Not Found" to the Package_Name "ListBox" column.

Here's the select statement:
"SELECT n.Workstation_Name , s.install_status , s.install_date , s.name , s.version , s.build , s.Package_Name , n.city , n.state FROM software s , network n where ( s.mac=n.mac AND n.Workstation_Name='B001617CA709E' ) AND ( s.name='Adobe Reader' AND s.Install_status='1') ORDER BY n.Workstation_Name ASC"

My ListBox control is named lstQueryResults and uses a Structure that is one to one with each row of the lstQueryResults control.
i.e.


Public Class LIST_CTRL_DATA

Public WksName As String

Public InstallStatus As String

Public InstallDate As String

Public PackageName As String

Public AppName As String

Public Version As String

Public Build As String

Public City As String

Public State As String

Public Percent As UInteger

Finally, here's the While..End routine that plugs the data into the ListBox called lstQueryResults.


While sqlReader.Read()

currec += 1

Dim msg As THREAD_MESSAGE

msg.msgid = 101

Dim message As New LIST_CTRL_DATA

message.WksName = sqlReader("Workstation_Name").ToString

message.InstallStatus = sqlReader("Install_Status").ToString

message.InstallDate = sqlReader("Install_Date").ToString

message.AppName = sqlReader("Name").ToString

message.Version = sqlReader("Version").ToString

message.Build = sqlReader("Build").ToString

message.PackageName = sqlReader("Package_Name").ToString

message.City = sqlReader("City").ToString

message.State = sqlReader("State").ToString

message.Percent = (currec * 100) / maxrecs

msg.msgdata = message

m_ParentWindow.Invoke(m_NotifyMainWindow, msg)

End While

Any ideas on whether or not you can write an If..Then Else statement within the SQL Select Statement and if so how?
I'm still somewhat new to SQL. Any assistance would be great.

Regards,
NBK

View 1 Replies View Related

How To Write The Select Statement?

Sep 26, 2007

I create a table that is named T1(C1,C2) and insert some data,as following:

C1 C2
1 1
4 NULL
NULL 5
7 NULL
NULL 9
12 12
14 NULL
NULL 16
I want to write a select statement to obtain the following result:

C1 C2
1 1
4 5
7 9
12 12
14 16
how to write the select statement?

 

View 2 Replies View Related

Cannot Set A Variable From A Select Statement That Contains A Variable??? Help Please

Oct 4, 2006

I am trying to set a vaiable from a select statement

DECLARE @VALUE_KEEP NVARCHAR(120),

@COLUMN_NAME NVARCHAR(120)



SET @COLUMN_NAME = (SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'CONTACTS' AND COLUMN_NAME = 'FIRSTNAME')



SET @VALUE_KEEP = (SELECT @COLUMN_NAME FROM CONTACTS WHERE CONTACT_ID = 3)



PRINT @VALUE_KEEP

PRINT @COLUMN_NAME



RESULTS

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

FirstName <-----------@VALUE_KEEP

FirstName <-----------@COLUMN_NAME



SELECT @COLUMN_NAME FROM CONTACTS returns: FirstName

SELECT FirstName from Contacts returns: Brent



How do I make this select statement work using the @COLUMN_NAME variable?

Any help greatly appreciated!

View 2 Replies View Related

Error While Trying To Assign A Value To A Read Write Variable In SSIS Package Script Component

Sep 26, 2006

Hi,

       I am trying to develop a SSIS package which will read the records from the flat file and insert them into a destination table. I have some validations written in script component. I have declared two Read Write variables with package level scope. when i try to assign a value to the variable in the script component and run the package, the package throws me an error "The collection of variables locked for read and write access is not available outside of PostExecute".

 

What should be done to over come the problem please help me on this regard 

 

Thanks

Madhavan.M

View 15 Replies View Related

How To Write Date Range On Case Statement

Oct 30, 2012

I have a case statement as follows which I am using in the where clause to get today's data

(Case when (A.ID is null or B.ID = '' or C.ID= ' ') Then convert(varchar,(Case when A.Time is null then B.Time else A.Time end),103)

else convert(varchar,(Case when A.Time is null then ''" else B.Time end),103) end)

=convert(varchar,getDATE(),103)

However I want to know whether I can use a date range on a case statement

I.e. I want to be able to use BETWEEN statement to get a date range

BETWEEN convert(varchar,getDATE(),103) AND convert(varchar,getDATE()-1,103)

How do I achieve this on a CASE WHEN statement?

View 3 Replies View Related

How To Write Multiple Queries In Case Statement

Sep 19, 2013

I have to do following scenario,

if 1st query Then 2nd Query
Else 'Msg'

How Can i do this using Case Statement??how can do this by Other way??

View 5 Replies View Related

How To Write A Delete Statement In Linked Server

Oct 6, 2007

Hi all,
We are retriving tables from DB2 and loading into a SQL server. while retriving temporary tables are getting created in the linked server. we need to delete the data in those temporary tables in Linked server. I don't know how to write delete statement for the tables in linked server. any pointers would be appreciated.

Thanks in advance.
Shriram

View 1 Replies View Related

How To Put Condition In Select Statement To Write A Cursor

Mar 29, 2008

col1 col2 col3 col4
36930.60 145 N . 00
17618.43 190 N . 00
6259.20 115 N .00
8175.45 19 N .00
18022.54 212 N .00
111.07 212 B .00
13393.05 67 N .00
In above 4 col
if col3 value is B then cursor has to fectch appropriate value from col4.
if col3 value is N then cursor has to fectch appropriate value from col1.
here col2 values are unique.

Can any one reply for this..............

View 3 Replies View Related

I Want To Write An SQL Statement Which Returns Matching Values But Ignores The First 2 Digits Of The Search

Jan 26, 2007

I want to write a statement something like this
SELECT Add_Date, File_No FROM dbo.File_Storage WHERE (File_No = 11/11/1234/)
But i want the search to ignore the first 2 digits so that it will return e.g
10/11/1234, 09/11/1234  so that it's only matching the last part
Any Help Would be greatly appreciated Thanks

View 6 Replies View Related

Transact SQL :: Insert Values From Variable Into Table Variable

Nov 4, 2015

CREATE TABLE #T(branchnumber VARCHAR(4000))

insert into #t(branchnumber) values (005)
insert into #t(branchnumber) values (090)
insert into #t(branchnumber) values (115)
insert into #t(branchnumber) values (210)
insert into #t(branchnumber) values (216)

[code]....

I have a parameter which should take multiple values into it and pass that to the code that i use. For, this i created a parameter and temporarily for testing i am passing some values into it.Using a dynamic SQL i am converting multiple values into multiple records as rows into another variable (called @QUERY). My question is, how to insert the values from variable into a table (table variable or temp table or CTE).OR Is there any way to parse the multiple values into a table. like if we pass multiple values into a parameter. those should go into a table as rows.

View 6 Replies View Related

Batch File - Read From One Table And Write To Another Database And Table

Nov 17, 2011

Any easy way for a batch file or automated process to read from one db and table and what ever entry is missing out of another database + table it writes those missing entries to.

This is a simple table in one db that is filled with usernames, I want to see if there are missing usernames in another db and table and write those entries.

db1.usr_table.usr_name = jdoenew
If jdoenew is missing in the 2nd db I will need to write entries like:
db1.usr_table.usr_name = jdoenew
db1.usr_table.password = tmppassword
db1.usr_table.active = 1

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved